Displaying pop-up windows in Python (PyQt4)(在 Python (PyQt4) 中显示弹出窗口)
问题描述
我需要知道如何在用户单击按钮时弹出对话框.
I need to know how to be able to make a dialog pop-up when a user clicks a button.
我对 Python 和 PyQt/QtDesigner 都比较陌生.我只在其中使用了大约一个月,但我认为我掌握得很好.
I'm relatively new to both Python and PyQt/QtDesigner. I've only been using in them for about a month, but I think I have a good grasp.
这是我所拥有的:我在 QtDesigner 中设计的主对话框(它是应用程序的主要部分).我使用 pyuic4easy 将 .ui 转换为 .py.
Here's what I have: A main dialog (which is the main part of the application), which I designed in QtDesigner. I converted the .ui to .py using pyuic4easy.
这就是我想要做的:在 QtDesigner 中设计一个新对话框,并以某种方式使其在用户单击第一个(主)对话框上的按钮时弹出.
Here's what I want to do: design a new dialog box in the QtDesigner and somehow make it pop up when a user clicks a button on the first (main) dialog.
这是我的主对话框的代码:
Here's the code for my main dialog:
import sys
from PyQt4.QtCore import *
from loginScreen import *
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL('clicked()'), self.popup)
...
... Some functions ...
def popup(self):
#Pop-up the new dialog
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= MyForm()
myapp.show()
sys.exit(app.exec_())
如您所见,我已将第一个按钮连接到一个名为popup"的方法,该方法需要填写代码以使我的第二个窗口弹出.我该怎么做呢?请记住,我已经在 QtDesigner 中设计了第二个对话框,我不需要创建新的.
So as you can see, I've connected the first button to a method named 'popup', which needs to be filled in with code to make my second window pop up. How do I go about doing this? Remember that I already have designed my second dialog in QtDesigner, and I don't need to create a new one.
感谢大家的帮助!
推荐答案
如您所见,我已将第一个按钮连接到名为'popup',需要填写代码才能使我的第二个弹出窗口.我该怎么做?
So as you can see, I've connected the first button to a method named 'popup', which needs to be filled in with code to make my second window pop up. How do I go about doing this?
与主窗口 (MyForm) 的操作方式几乎相同.
Pretty much the same way you do it for your main window (MyForm).
像往常一样,您为第二个对话框的 QtDesigner 代码编写一个包装类(就像您对 MyForm 所做的那样).我们称之为MyPopupDialog.然后在您的 popup 方法中,您创建一个实例,然后使用 exec_() 或 show() 显示您的实例,具体取决于您是否想要模态或非模态对话框.(如果不熟悉 Modal/Modeless 的概念,可以参考 文档.)
As usual, you write a wrapper class for your QtDesigner code for the second dialog (like you did with MyForm). Let's call it MyPopupDialog. Then in your popup method, you create an instance and then show your instance with either exec_() or show() depending whether you want a modal or modeless dialog. (If you are not familiar with Modal/Modeless concept, you might refer to the documentation.)
所以整体可能看起来像这样(有一些修改):
So the overall thing might look like this (with a couple of modifications):
# Necessary imports
class MyPopupDialog(QtGui.QDialog):
def __init__(self, parent=None):
# Regular init stuff...
# and other things you might want
class MyForm(QtGui.QDialog):
def __init__(self, parent=None):
# Here, you should call the inherited class' init, which is QDialog
QtGui.QDialog.__init__(self, parent)
# Usual setup stuff
self.ui = Ui_Dialog()
self.ui.setupUi(self)
# Use new style signal/slots
self.ui.pushButton.clicked.connect(self.popup)
# Other things...
def popup(self):
self.dialog = MyPopupDialog()
# For Modal dialogs
self.dialog.exec_()
# Or for modeless dialogs
# self.dialog.show()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp= MyForm()
myapp.show()
sys.exit(app.exec_())
这篇关于在 Python (PyQt4) 中显示弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python (PyQt4) 中显示弹出窗口
基础教程推荐
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
