問題描述
我需要知道如何在用戶單擊按鈕時彈出對話框.
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) 中顯示彈出窗口的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!