如何在PyQT4上显示消息框?
我希望当我单击简单的 PyQT 应用程序上的按钮时显示一个消息框。如何声明两个文本框并让 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_())
I'd like a MessageBox to display when I click a button on my simple PyQT application. How can I declare two textboxes and have a MessageBox display with the text from both textboxes?
Here's my code far:
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_())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于这样简单的代码是一个常见的请求,所以我决定将一些基本的东西放在一起,如下所示:
在行编辑(文本框)中写入一些内容,单击按钮。利润! :-)
注意:它可以用更少的代码来完成,但这是一个很好的 PyQt 编码实践 - 创建一个小部件作为窗口的中央小部件,用布局填充它等。
Since such simple code is a common request, I decided to hack something basic together, here you go:
Write something into the line edits (text boxes), click the button. Profit! :-)
Note: it can be done with less code, but this is a good PyQt coding practice - create a widget to serve as a central widget of a window, populate it with a layout, etc.
PyQt 在安装时附带了示例。这些示例包含许多非常有用的代码,您可以从中学习,也可以获取整个代码块并使用它们。
例如,查看“通讯录”示例,该示例会弹出消息框以及其他内容(在其来源中搜索“messagebox”)。
PyQt comes with examples when you install it. These examples contain a lot of very useful code and you can learn from them, as well as take whole code chunks and use them.
Check out, for example, the "Address book" example which pops message boxes along other things (search its sources for "messagebox").
来源:
Source: