Python Can';似乎无法从主窗口调用自定义对话框。皮塞德
我需要通过主窗口(同样在QtDesigned上,因此是单独的文件)上的按钮槽调用表单(使用QtDesigner设计的自定义对话框)。以下是相关代码:Python Can';似乎无法从主窗口调用自定义对话框。皮塞德,python,qt,pyqt,pyside,qt-designer,Python,Qt,Pyqt,Pyside,Qt Designer,我需要通过主窗口(同样在QtDesigned上,因此是单独的文件)上的按钮槽调用表单(使用QtDesigner设计的自定义对话框)。以下是相关代码: def __init__(self, parent = None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.btn.clicked.connect(self.my_func) def my_func(self): fo
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn.clicked.connect(self.my_func)
def my_func(self):
form = Form_UI.Custom_Dialog()
if form.exec_():
print "successfully opened"
我如何得到以下错误:
Traceback (most recent call last):
File "F:\myPath\code.py", line 27, in my_func
if form.exec_():
AttributeError: 'Custom_Dialog' object has no attribute 'exec_'
我不明白,因为以下代码(使用内置对话框)工作正常:
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.btn.clicked.connect(self.my_func)
def my_func(self):
form = QtGui.QDialog()
if form.exec_():
print "successfully opened"
任何帮助都将不胜感激。提前感谢。由
pyuic4
生成的类不是从QDialog
派生的,因此如果您没有像为主窗口那样为该ui文件编写python类,则需要创建QDialog
对象和ui类对象:
def my_func(self):
form = QtGui.QDialog()
ui_form = Form_UI.Custom_Dialog()
ui_form.setupUi(form)
if form.exec_():
print "successfully opened"
太棒了,我真的很感激。谢谢。但是,我必须问一下,在ui文件中导出它或如上所述设置它,更好的做法(或约定)是什么?@Ryan因为您无法编辑生成的文件,因为再次调用pyuic4时任何更改都会丢失,我想您的意思是"... 编写一个类,该类将从
QDialog
和Custom_Dialog
或如上所述的设置中派生。除了常规方法外,这两种方法之间没有PyQt约定可供选择,因此,基本上,如果您不需要向生成的对话框添加任何逻辑(相对复杂的用户交互或验证…),以上代码应该足够了。