PyQt4:创建返回参数的自定义对话框
我正在尝试向当前的 GUI 添加一个自定义对话框,用户可以启动该对话框来设置一些参数。理想情况下,我想使用 QtDesigner 创建自定义对话框。下面是 pyuic4
从 QtDesigner ui 代码中为对话框生成的代码。
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(508, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 120, 181, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.sl_value = QtGui.QSlider(Dialog)
self.sl_value.setGeometry(QtCore.QRect(220, 120, 161, 31))
self.sl_value.setOrientation(QtCore.Qt.Horizontal)
self.sl_value.setObjectName("sl_value")
self.ed_value = QtGui.QLineEdit(Dialog)
self.ed_value.setGeometry(QtCore.QRect(400, 120, 41, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.ed_value.setFont(font)
self.ed_value.setObjectName("ed_value")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "Set example value:", None, QtGui.QApplication.UnicodeUTF8))
这保存在 Sub2.py
中 然后,在我的主 python 文件中,我添加了
from Sub2 import Ui_Dialog
一个名为 StartSub2
的新类,其中包含以下代码
class StartSub2(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.ui = Ui_Dialog
self.ui.setupUi(self)
最后,在我的主 GUI 中,有一个包含以下代码的函数,该函数应启动该对话框
def exampleSubGui(self):
dialog = StartSub2(self)
result = dialog.exec_()
请注意对话框尚未完成。一旦我解决了如何启动它,我将为滑块和编辑框添加信号/插槽连接。另外,如果我理解正确的话,我需要重载 accept()
方法来返回用户的输入。
我遇到的第一个问题是 StartSub2
的 __init__
方法。我收到以下错误:
TypeError: unbound method setupUi() must be called with Ui_Dialog instance as
first argument (got StartSub2 instance instead)
我尝试采用与主 GUI 相同的方法,该方法使用以下代码
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
但这并不会抱怨 setupUi()
获取 StartQT4 实例而不是
Ui_MainWindow
实例。谁能解释完成我想做的事情的正确方法?或者有人可以给我指出一个明确的例子或参考吗?如果您需要更多信息或澄清,请告诉我。
I'm attempting to add a custom dialog box to my current GUI that can be launched for the user to set some parameters. Ideally, I would like to create the custom dialog using QtDesigner. Below is the code generated by pyuic4
from the QtDesigner ui code for the dialog box.
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(508, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(10, 120, 181, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.sl_value = QtGui.QSlider(Dialog)
self.sl_value.setGeometry(QtCore.QRect(220, 120, 161, 31))
self.sl_value.setOrientation(QtCore.Qt.Horizontal)
self.sl_value.setObjectName("sl_value")
self.ed_value = QtGui.QLineEdit(Dialog)
self.ed_value.setGeometry(QtCore.QRect(400, 120, 41, 31))
font = QtGui.QFont()
font.setPointSize(16)
self.ed_value.setFont(font)
self.ed_value.setObjectName("ed_value")
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "Set example value:", None, QtGui.QApplication.UnicodeUTF8))
This is saved in Sub2.py
Then, in my main python file, I add
from Sub2 import Ui_Dialog
I create a new class called StartSub2
with the following code
class StartSub2(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.ui = Ui_Dialog
self.ui.setupUi(self)
Then finally in inside my main GUI there's a function with the following code that should launch the dialog
def exampleSubGui(self):
dialog = StartSub2(self)
result = dialog.exec_()
Please note that the dialog is not done. Once I resolve how to even launch it I will add signal/slot connections for the slider and edit box. Also, if I understand it correctly, I need to overload the accept()
method to return the user's input.
The first problem I run into is with the __init__
method of StartSub2
. I get the following error:
TypeError: unbound method setupUi() must be called with Ui_Dialog instance as
first argument (got StartSub2 instance instead)
I'm attempting to take the same approach that the main GUI is taking which uses the following code
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
But this does not complain about setupUi()
getting a StartQT4
instance instead of a Ui_MainWindow
instance. Can anyone explain the proper way to accomplish what I'm trying to do? Or can somebody point me to a clear example or reference? Please let me know if you need more information or clarification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该解决让对话框初始化的第一个问题。
为了获取信息,我通常会向
StartSub2
添加一个名为getValues
的方法,然后执行以下操作
should resolve your first problem of getting the dialog to initialize.
To get info back I usually add a method called something like
getValues
toStartSub2
, i.e.then do
只是想提供我自己的答案,用于设置带有返回值的自定义对话框(不回答代码特定问题,Whatang 已经这样做了)。
我发现使用 类方法< 构建一个简单的对话框类会更干净一些/a> 可以根据需要返回不同的东西。我最近做了很多这些!这个想法是,类方法将构造对话框类的实例,并从实例中返回对象(在本例中为 bool
ok
),这或多或少是一个 工厂方法(根据我的理解,对于OOP来说仍然有些陌生)。这是一个非常简化的示例对话框。将其扩展到对话框类中所需的任何内容应该相对容易:
美妙之处在于,当需要构建此对话框时,您只需一行即可完成
OkDialog.isOkay(parent)
。将其组合成一个完整的工作示例:Just wanted to provide my own answer for setting up a custom dialog with return values (not answering the code specific questions, which Whatang already did).
I found it a bit cleaner to construct a simple dialog class with a class method that can return different things as needed. I've been making these a lot lately! The idea is that the class method will construct an instance of the dialog class, and return objects out of the instance (in this case the bool
ok
) which is more or less a factory method (from what I understand anyway, still somewhat new to OOP).Here's a very simplified example dialog. It should be relatively easy to extend this to whatever you need within the dialog class:
The beauty is that when it it comes time to construct this dialog you can do it with just one line
OkDialog.isOkay(parent)
. Putting it together into a fully working sample: