python - 按下 QPushButton 时打印控制台消息

标签 python pyqt4 qpushbutton

我是 Python 新手,我想编写一个简单的代码,以便在按下 QPushButton 时在 Python 控制台中打印一条消息。我将代码分成 2 个 .py 文件(一个用于 UI,另一个用于主应用程序)。虽然应该很简单,但我还是没能成功。

我很确定这是一个初学者的错误,但当我检查相关帖子的答案,甚至与本论坛其他帖子中的类似代码进行比较时,我找不到问题所在。

“从主程序测试!”、“从主程序测试 - 第二次!”和“从编辑中测试!”控制台上出现了打印行,但我不知道为什么单击按钮时不调用其他两行。我猜我在 clicked.connect() 方法上做错了什么。

非常感谢你们。

该文件的代码如下:

用户界面代码:

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 实例未在代码中的任何位置声明。您正在调用该类的 init 中的函数,但该类尚未实例化。

在这些情况下,最好在 Ui_Dialog 类中创建一个函数,在单击按钮时实例化该类,并调用构造函数中的函数来打印结果。

关于python - 按下 QPushButton 时打印控制台消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30369408/

相关文章:

python - 用于忽略字符串中括号的正则表达式

python - Reportlab 将报告合并为一个 PDF

python - 当我尝试通过 django 发送电子邮件时出现 Gmail SMTPAuthenticationError

c++ - 触摸屏Qt应用

python - QPushButton 不显示

qt - QPushButton 100% QGridLayout 单元格的高度

python - 如何将父类方法的内容添加到子类方法中

python-2.7 - PyQt 4 : Get Position of Toolbar

python - 你会如何使用 Python 进行广告拦截?

python-2.7 - 从 PyQt4 信号转换为 PyQt5 信号不起作用