Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PyQT4上显示消息框?_Python_Pyqt - Fatal编程技术网

Python 如何在PyQT4上显示消息框?

Python 如何在PyQT4上显示消息框?,python,pyqt,Python,Pyqt,我希望在单击简单PyQT应用程序上的按钮时显示MessageBox。如何声明两个文本框,并使MessageBox显示来自两个文本框的文本 以下是我的代码: import sys from PyQt4 import QtGui, QtCore class myWindow(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) #The s

我希望在单击简单PyQT应用程序上的按钮时显示MessageBox。如何声明两个文本框,并使MessageBox显示来自两个文本框的文本

以下是我的代码:

import sys
from PyQt4 import QtGui, QtCore

class myWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        #The setGeometry method is used to position the control.
        #Order: X, Y position - Width, Height of control.
        self.setGeometry(300, 300, 500, 350)
        self.setWindowTitle("Sergio's QT Application.")
        self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png'))

        self.setToolTip('<i>Welcome</i> to the <b>first</b> app ever!')
        QtGui.QToolTip.setFont(QtGui.QFont('Helvetica', 12))

        txtFirstName = QtGui.?
        txtLastName = QtGui.?

        btnQuit = QtGui.QPushButton('Exit Application', self)
        btnQuit.setGeometry(340, 300, 150, 35)

        self.connect(btnQuit, QtCore.SIGNAL('clicked()'),
                    QtGui.qApp, QtCore.SLOT('quit()'))

app = QtGui.QApplication(sys.argv)
mainForm = myWindow()
mainForm.show()
sys.exit(app.exec_())
导入系统 从PyQt4导入QtGui、QtCore 类myWindow(QtGui.QWidget): def uuu init uuu(self,parent=None): QtGui.QWidget.\uuuuu init\uuuuuu(self,parent) #setGeometry方法用于定位控件。 #顺序:X,Y位置-控制的宽度和高度。 self.setGeometry(300300500350) setWindowTitle(“Sergio的QT应用程序”) self.setWindowIcon(QtGui.QIcon('menuScreenFolderShadow.png')) self.setToolTip('欢迎使用有史以来第一个应用!') QtGui.QToolTip.setFont(QtGui.QFont('Helvetica',12)) txtFirstName=QtGui。? txtLastName=QtGui。? btnQuit=QtGui.QPushButton('Exit Application',self) b查询集合几何(34030015035) self.connect(btnQuit,QtCore.SIGNAL('clicked()'), QtGui.qApp,QtCore.SLOT('quit()')) app=QtGui.QApplication(sys.argv) mainForm=myWindow() mainForm.show() sys.exit(app.exec_())
PyQt在安装时附带了一些示例。这些示例包含许多非常有用的代码,您可以从中学习,也可以使用整个代码块


例如,查看“地址簿”示例,它会弹出消息框以及其他内容(搜索其来源以查找“messagebox”)。

由于这样简单的代码是一个常见的请求,我决定一起破解一些基本的东西,如下所示:

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.create_main_frame()       

    def create_main_frame(self):        
        page = QWidget()        

        self.button = QPushButton('joy', page)
        self.edit1 = QLineEdit()
        self.edit2 = QLineEdit()

        vbox1 = QVBoxLayout()
        vbox1.addWidget(self.edit1)
        vbox1.addWidget(self.edit2)
        vbox1.addWidget(self.button)
        page.setLayout(vbox1)
        self.setCentralWidget(page)

        self.connect(self.button, SIGNAL("clicked()"), self.clicked)

    def clicked(self):
        QMessageBox.about(self, "My message box", "Text1 = %s, Text2 = %s" % (
            self.edit1.text(), self.edit2.text()))



if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()
在行编辑(文本框)中写入内容,单击按钮。利润!:-)

注意:这可以用更少的代码完成,但这是一个很好的PyQt编码实践-创建一个小部件作为窗口的中心小部件,用布局填充它,等等。

资料来源:


  • 我在哪里可以找到这些例子?我使用二进制安装程序在Windows上安装了PyQT。谢谢。在“开始”菜单中尝试->查找PyQT安装的链接,它是there@Serg:一个直接链接是(对于Python2.6,如果您的版本不同,请进行调整):C:\Python26\Lib\site packages\PyQt4\example汉克斯,我找到了示例。不幸的是,我似乎找不到任何与消息框相关的东西。你能发布一些针对这个问题的小代码吗?谢谢您的时间。@Serg:请看我对这个问题的其他回答()
    from PyQt4 import QtGui, QtCore
    
    class Window( QtGui.QWidget ):
    
        def __init__( self ):
            QtGui.QWidget.__init__( self )
    
            msgBox = QtGui.QMessageBox( self )
            msgBox.setIcon( QtGui.QMessageBox.Information )
            msgBox.setText( "Do not stare into laser with remaining eye" )
    
            msgBox.setInformativeText( "Do you really want to disable safety enforcement?" )
            msgBox.addButton( QtGui.QMessageBox.Yes )
            msgBox.addButton( QtGui.QMessageBox.No )
    
            msgBox.setDefaultButton( QtGui.QMessageBox.No ) 
            ret = msgBox.exec_()
    
            if ret == QtGui.QMessageBox.Yes:
                print( "Yes" )
                return
            else:
                print( "No" )
                return
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication( sys.argv )
        window = Window()
        # window.show()
        sys.exit( app.exec_() )