我希望有一个使用qt4的python 2程序,当在QMainWindow中按下按钮时,它会打印到终端。如何在QT4 Designer创建的QMainWindow中为按钮创建插槽/信号?
我已经尝试了我能想到的一切,但我不明白如何将我在网上找到的内容应用于此问题。我对python很新,很可能是原因,如果这很明显,请原谅我,但我很困惑。我发现的教程是qt4 for C ++,而不是python。如果不出意外,至少下一个人将能够从中学习。
我得到的错误是:
$ python ./main.py
Traceback (most recent call last):
File "./main.py", line 27, in <module>
myGUI.connect(myGUI.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")),pushButtonCLICK)
AttributeError: 'QMainWindow' object has no attribute 'pushButton'
main.py :
#!/usr/bin/env python
import sys
import PyQt4
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout, QMainWindow
import gui
def pushButtonCLICK():
print("you pressed the button! Yay! \n")
def main(args):
return 0
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
myGUI = QtGui.QMainWindow()
ui = gui.Ui_MainWindow()
ui.setupUi(myGUI)
myGUI.show()
myGUI.connect(myGUI.pushButton,QtCore.SIGNAL(_fromUtf8("clicked()")),pushButtonCLICK)
sys.exit(app.exec_())
在QT4 Designer中设计QMainWindow
,另存为&#34; .ui&#34;使用以下文件转换为python:
pyuic4 -x ./test.ui -o ./gui.py
gui.ui::
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>333</width>
<height>245</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>120</x>
<y>50</y>
<width>85</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>ClickHere</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>333</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
gui.py :
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file './test.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(333, 245)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.pushButton = QtGui.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(120, 50, 85, 30))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 333, 26))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "ClickHere", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
#!/usr/bin/env python
import sys
import PyQt4
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QDialog, QApplication, QPushButton, QLineEdit, QFormLayout, QMainWindow
import gui
def pushButtonCLICK():
print("you pressed the button! Yay! \n")
def main(args):
return 0
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
myGUI = QtGui.QMainWindow()
ui = gui.Ui_MainWindow()
ui.setupUi(myGUI)
myGUI.show()
myGUI.connect(ui.pushButton,QtCore.SIGNAL("clicked()"),pushButtonCLICK)
sys.exit(app.exec_())