连接pyqt信号与另一个类
问题描述:
是否有可能让我得到pyqt信号触发另一个类的方法? 我尝试了各种各样,但没有运气。 我的目标是在单击room_file_button(标记)时触发get_rooms中的pickFile()方法。连接pyqt信号与另一个类
import sys
from PyQt4 import QtCore, QtGui, uic
import openpyxl
from openpyxl import load_workbook
from openpyxl.styles import Protection
import xlrd
import csv
import os
import re
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.room_file_button.clicked.connect(get_rooms.pickFile) # this one
self.radioButton_1.clicked.connect(self.onRadioButton1)
self.radioButton_2.clicked.connect(self.onRadioButton2)
self.radioButton_3.clicked.connect(self.onRadioButton3)
self.spinBox.valueChanged.connect(self.valuechange)
class first_file(MyApp):
def __init__(self):
MyApp.__init__(self)
some methods ....
class get_rooms(MyApp):
def __init__(self):
MyApp.__init__(self)
def pickFile(self, value, group_1):
print 'yipeee !'
xy = 0
while True:
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
if filename == '' and xy < 2:
print(" ")
xy = xy + 1
continue
elif filename != '':
break
else:
sys.exit()
答
首先,可以设置pickFile
用作静态函数(无自我):
class get_rooms(MyApp):
def __init__(self):
MyApp.__init__(self)
@staticmethod
def pickFile(value, group_1):
,然后就可以使用room_file_button.clicked
信号;如果你想将参数发送到该功能,您可以使用lambda
:
self.room_file_button.clicked.connect(lambda: get_rooms.pickFile(myValue,myGrupe))
为什么是类'first_file'和'get_rooms'从'MyApp'继承?你想达到什么目的?请多给点信息。 – DreyFax