按下 QPushButton 时打印控制台消息
Printing console message when QPushButton is pressed
我是 Python 的新手,我想编写一个简单的代码来在按下 QPushButton 时在 Python 控制台中打印一条消息。我将代码拆分为 2 个 .py 文件(一个用于 UI,另一个用于主应用程序)。虽然应该很简单,但我还是没有成功。
我很确定这是初学者的错误,但是当我检查相关帖子的答案甚至与本论坛其他帖子的类似代码进行比较时,我都找不到问题所在。
"Test from main!"、"Test from main - 2nd time!" 和 "Test from edit!" 打印行出现在控制台上,但我不知道为什么单击按钮时没有调用其他两个。我猜我在使用 clicked.connect() 方法时做错了什么。
非常感谢大家。
文件代码如下:
UI代码:
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(850, 450)
self.pushButton_2 = QtGui.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(100, 60, 251, 61))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_Selection = QtGui.QPushButton(Dialog)
self.pushButton_Selection.setGeometry(QtCore.QRect(148, 250, 161, 61))
self.pushButton_Selection.setObjectName(_fromUtf8("pushButton_Selection"))
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Communication", None))
self.pushButton_2.setText(_translate("Dialog", "Print Line", None))
self.pushButton_Selection.setText(_translate("Dialog", "Select sensor", None))
print "Test from edit!"
主文件代码:
import sys
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
from EditTxt_ui import Ui_Dialog
print "Test from main!"
class MyForm(QtGui.QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.ui.setupUi(self)
self.pushButton_2.clicked.connect(self.print_LineEdit)
self.pushButton_Selection.clicked.connect(self.print_Selection)
print "Test from main - 2nd time!"
def print_LineEdit(self):
print "Print from LineEdit!"
def print_Selection(self):
print "Print Selection!"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
您收到此错误是因为代码中的任何地方都没有声明 MyForm 实例。您正在调用 class' init 中的函数,但 class 未实例化。
在这些情况下,最好在 Ui_Dialog class 中创建一个函数,在单击按钮时实例化 class 并调用构造函数中的函数,打印结果。
您的代码中存在两个问题:
您没有创建 class MyForm 的实例。
按钮是在 class Ui_Dialog 中创建的,但您尝试从 MyForm 访问它们。
如果你想访问 MyForm 中的按钮,你必须在 MyForm 中创建一个 Ui_Dialog 的实例,如下所示:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton_2.clicked.connect(self.print_LineEdit)
self.ui.pushButton_Selection.clicked.connect(self.print_Selection)
def print_LineEdit(self):
print "Print from LineEdit!"
def print_Selection(self):
print "Print Selection!"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = MyForm()
MainWindow.show()
sys.exit(app.exec_())
另一个解决方案是将函数 print_LineEdit 和 print_Selection 放在 class Ui_Dialog.
中
但是对于像这样一个非常简单的应用程序,我会将主文件中的所有内容放在一个 class 中,如下所示:
import sys
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
class MyForm(QtGui.QWidget):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.pushButton_2=QtGui.QPushButton("Print Line")
self.pushButton_Selection=QtGui.QPushButton("Select sensor")
self.pushButton_2.clicked.connect(self.print_LineEdit)
self.pushButton_Selection.clicked.connect(self.print_Selection)
#layout
vbox=QtGui.QVBoxLayout()
vbox.addWidget(self.pushButton_2)
vbox.addWidget(self.pushButton_Selection)
self.setLayout(vbox)
def print_LineEdit(self):
print "Print from LineEdit!"
def print_Selection(self):
print "Print Selection!"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = MyForm()
MainWindow.show()
sys.exit(app.exec_())
(我没有在意布局,但你可以)
我是 Python 的新手,我想编写一个简单的代码来在按下 QPushButton 时在 Python 控制台中打印一条消息。我将代码拆分为 2 个 .py 文件(一个用于 UI,另一个用于主应用程序)。虽然应该很简单,但我还是没有成功。
我很确定这是初学者的错误,但是当我检查相关帖子的答案甚至与本论坛其他帖子的类似代码进行比较时,我都找不到问题所在。
"Test from main!"、"Test from main - 2nd time!" 和 "Test from edit!" 打印行出现在控制台上,但我不知道为什么单击按钮时没有调用其他两个。我猜我在使用 clicked.connect() 方法时做错了什么。
非常感谢大家。
文件代码如下:
UI代码:
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
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_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(850, 450)
self.pushButton_2 = QtGui.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(100, 60, 251, 61))
self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
self.pushButton_Selection = QtGui.QPushButton(Dialog)
self.pushButton_Selection.setGeometry(QtCore.QRect(148, 250, 161, 61))
self.pushButton_Selection.setObjectName(_fromUtf8("pushButton_Selection"))
self.retranslateUi(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Communication", None))
self.pushButton_2.setText(_translate("Dialog", "Print Line", None))
self.pushButton_Selection.setText(_translate("Dialog", "Select sensor", None))
print "Test from edit!"
主文件代码:
import sys
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
from EditTxt_ui import Ui_Dialog
print "Test from main!"
class MyForm(QtGui.QMainWindow, Ui_Dialog):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.ui.setupUi(self)
self.pushButton_2.clicked.connect(self.print_LineEdit)
self.pushButton_Selection.clicked.connect(self.print_Selection)
print "Test from main - 2nd time!"
def print_LineEdit(self):
print "Print from LineEdit!"
def print_Selection(self):
print "Print Selection!"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
您收到此错误是因为代码中的任何地方都没有声明 MyForm 实例。您正在调用 class' init 中的函数,但 class 未实例化。
在这些情况下,最好在 Ui_Dialog class 中创建一个函数,在单击按钮时实例化 class 并调用构造函数中的函数,打印结果。
您的代码中存在两个问题:
您没有创建 class MyForm 的实例。
按钮是在 class Ui_Dialog 中创建的,但您尝试从 MyForm 访问它们。
如果你想访问 MyForm 中的按钮,你必须在 MyForm 中创建一个 Ui_Dialog 的实例,如下所示:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton_2.clicked.connect(self.print_LineEdit)
self.ui.pushButton_Selection.clicked.connect(self.print_Selection)
def print_LineEdit(self):
print "Print from LineEdit!"
def print_Selection(self):
print "Print Selection!"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = MyForm()
MainWindow.show()
sys.exit(app.exec_())
另一个解决方案是将函数 print_LineEdit 和 print_Selection 放在 class Ui_Dialog.
中但是对于像这样一个非常简单的应用程序,我会将主文件中的所有内容放在一个 class 中,如下所示:
import sys
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
class MyForm(QtGui.QWidget):
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.pushButton_2=QtGui.QPushButton("Print Line")
self.pushButton_Selection=QtGui.QPushButton("Select sensor")
self.pushButton_2.clicked.connect(self.print_LineEdit)
self.pushButton_Selection.clicked.connect(self.print_Selection)
#layout
vbox=QtGui.QVBoxLayout()
vbox.addWidget(self.pushButton_2)
vbox.addWidget(self.pushButton_Selection)
self.setLayout(vbox)
def print_LineEdit(self):
print "Print from LineEdit!"
def print_Selection(self):
print "Print Selection!"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
MainWindow = MyForm()
MainWindow.show()
sys.exit(app.exec_())
(我没有在意布局,但你可以)