PyQt 使用多个类分配按钮命令
我对 Python 和 PyQt 都是新手。当我有多个课程时,我在使某些按钮操作正常工作时遇到问题。例如,在我的代码中,我有以下内容:
class main(QDialog):
def __init__(self, parent=None, *args):
QDialog.__init__(self, parent, *args)
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.resize(610, 570)
Dialog.setWindowTitle(QtGui.QApplication.translate("testing",
"testing", None, QtGui.QApplication.UnicodeUTF8))
self.stackedWidget = QtGui.QStackedWidget(Dialog)
self.stackedWidget.setGeometry(QtCore.QRect(0, 120, 711, 450))
for i in range(1, 3):
self.__dict__['page%d' % i] = QtGui.QWidget()
self.stackedWidget.addWidget(self.__dict__['page%d' % i] )
self.Ui_Customer = Ui_Customer(Dialog, self.page1)
# QtCore.QObject.connect(self.Ui_Customer.pushButton_2,
# QtCore.SIGNAL("clicked()"), self.clearalltextboxes)
# def clearalltextboxes(self):
# for i in range(1, 13):
# self.Ui_Customer.__dict__['textEdit_%d' % i].clear()
self.stackedWidget.setCurrentIndex(0)
class Ui_Customer(main):
def __init__(self, Dialog, page):
for i in range(1, 3):
self.__dict__['text_%d' % i] = QtGui.QTextEdit(page)
self.text_1.setGeometry(QtCore.QRect(10, 10, 81, 21))
self.text_2.setGeometry(QtCore.QRect(240, 10, 81, 21))
self.pushButton_2 = QtGui.QPushButton(page)
self.pushButton_2.setGeometry(QtCore.QRect(130, 160, 101, 23))
self.pushButton_2.setText("Reset")
QtCore.QObject.connect(self.pushButton_2,
QtCore.SIGNAL("clicked()"), self.clearalltextboxes)
def clearalltextboxes(self):
for i in range(1, 3):
self.Ui_Customer.__dict__['textEdit_%d' % i].clear()
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = main()
myapp.show()
sys.exit(app.exec_())
问题是,当我按下“重置”按钮(pushbutton_2)时,什么也没有发生(我什至在 python 中没有收到任何类型的错误)。如果我从代码中删除注释部分,我就可以让它工作。我唯一的问题是我想将按钮操作命令保留在 Ui_Customer 类中。有办法做到这一点吗?
我最终将拥有几个使用 QtCore.QObject.connect 选项将操作分配给按钮的类。我只是在使第一个工作正常工作时遇到问题,而不必将其添加到 main() 类中。
I am new to Python as well as to PyQt. I am having problems getting some of the button actions to work when I have multiple classes. For example, in my code I have the following:
class main(QDialog):
def __init__(self, parent=None, *args):
QDialog.__init__(self, parent, *args)
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.resize(610, 570)
Dialog.setWindowTitle(QtGui.QApplication.translate("testing",
"testing", None, QtGui.QApplication.UnicodeUTF8))
self.stackedWidget = QtGui.QStackedWidget(Dialog)
self.stackedWidget.setGeometry(QtCore.QRect(0, 120, 711, 450))
for i in range(1, 3):
self.__dict__['page%d' % i] = QtGui.QWidget()
self.stackedWidget.addWidget(self.__dict__['page%d' % i] )
self.Ui_Customer = Ui_Customer(Dialog, self.page1)
# QtCore.QObject.connect(self.Ui_Customer.pushButton_2,
# QtCore.SIGNAL("clicked()"), self.clearalltextboxes)
# def clearalltextboxes(self):
# for i in range(1, 13):
# self.Ui_Customer.__dict__['textEdit_%d' % i].clear()
self.stackedWidget.setCurrentIndex(0)
class Ui_Customer(main):
def __init__(self, Dialog, page):
for i in range(1, 3):
self.__dict__['text_%d' % i] = QtGui.QTextEdit(page)
self.text_1.setGeometry(QtCore.QRect(10, 10, 81, 21))
self.text_2.setGeometry(QtCore.QRect(240, 10, 81, 21))
self.pushButton_2 = QtGui.QPushButton(page)
self.pushButton_2.setGeometry(QtCore.QRect(130, 160, 101, 23))
self.pushButton_2.setText("Reset")
QtCore.QObject.connect(self.pushButton_2,
QtCore.SIGNAL("clicked()"), self.clearalltextboxes)
def clearalltextboxes(self):
for i in range(1, 3):
self.Ui_Customer.__dict__['textEdit_%d' % i].clear()
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = main()
myapp.show()
sys.exit(app.exec_())
The problem is that when I press the "reset" button (pushbutton_2) nothing happens (I do not even get any kind of error within python). I can get it to work if I remove the commented sections out of my code. My only issue with that is that I was wanting to keep the button action command in the Ui_Customer class. Is there a way do do this?
I will eventually have several classes that the use the QtCore.QObject.connect option to assign an action to a button. I'm just having problems getting the first one to work without having to add it to the main() class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该按钮不起作用的原因是
Ui_Customer
已成为main
的子类(为什么?)并且未正确初始化。如果基类更改为object
,信号处理程序将工作 - 尽管它会由于字典查找中的拼写错误而生成AttributeError
(我认为你的意思是>'text_%d'
)。我不会评论代码中的任何单独问题,而只会给出一条一般性建议:使用 Qt Designer 创建 UI,并将所有程序逻辑保留在单独的模块中。它将使学习 PyQt 变得更加容易,并且是未来养成的好习惯。
The reason why the button doesn't work is because
Ui_Customer
has been made a subclass ofmain
(why?) and not initialized properly. If the base class is changed toobject
, the signal handler will work - although it will generate anAttributeError
due to a typo in the dict lookup (I think you meant'text_%d'
).Rather than comment on any of the individual issues with your code, I will just give one general piece of advice: create your UIs using Qt Designer and keep all your program logic in separate modules. It will make learning PyQt much easier, and is a good habit to get into for the future.