2017-02-24 91 views
0

我只是想创建一个小应用程序,它在按下按钮时打开另一个对话框。以下是对话框的代码。创建一个返回参数的自定义对话框

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)) 

而在主文件我已经添加下面的代码段

class StartSub2(QtGui.QDialog, Ui_Dialog): 
    def __init__(self,parent=None): 
     QtGui.QDialog.__init__(self,parent) 
     self.setupUi(self) 

dlg = StartSub2() 
if dlg.exec_(): 
    values = dlg.getValues() 

每当按下按钮时打开的对话框但是对话的是元件的完全空没有被示出。 那么如何让元素在对话框中可见? 但是,当我尝试导入主文件对话框的文件我收到错误

cannot import name Ui_Dialog 
+0

你可以表现出你所得到的 – eyllanesc

+0

你的代码已经发布的作品对我来说非常精细的图像。我只需要在创建对话框之前添加'app = QtGui.QApplication([])''。 – ekhumoro

回答

0

使用PyQt5(对不起)。首先,UI定义:

# file ui_dialog.py 

from PyQt5 import QtCore, QtGui, QtWidgets 

class Ui_Dialog(object): 
    def setupUi(self, Dialog): 
     Dialog.setObjectName("Dialog") 
     Dialog.resize(508, 300) 
     self.buttonBox = QtWidgets.QDialogButtonBox(Dialog) 
     self.buttonBox.setGeometry(QtCore.QRect(150, 250, 341, 32)) 
     self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 
     self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) 
     self.buttonBox.setObjectName("buttonBox") 
     self.sl_value = QtWidgets.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.buttonBox.accepted.connect(Dialog.accept) 
     self.buttonBox.rejected.connect(Dialog.reject) 
     QtCore.QMetaObject.connectSlotsByName(Dialog) 

,并在同一目录下,应用程序定义:

from PyQt5.QtWidgets import (QApplication, QDialog) 
from ui_dialog import Ui_Dialog 


class StartSub2(QDialog): 
    def __init__(self,parent=None): 
     QDialog.__init__(self,parent) 

     self.ui = Ui_Dialog() 
     self.ui.setupUi(self) 

     self.values = dict(slider=None) 

     self.slider = self.ui.sl_value 

     # connect the valueChanged signal to the update_slider slot 
     self.slider.valueChanged.connect(self.update_slider) 

    # slot 
    def update_slider(self, val): 
     self.values['slider'] = val 


if __name__ == '__main__': 
    import sys 

    app = QApplication(sys.argv) 
    dialog = StartSub2() 
    if dialog.exec(): 
     slider_value = dialog.values['slider'] 
+0

非常感谢你 –

+0

你非常欢迎。我希望它有帮助!如果答案足够了,请考虑标记为正确。欣赏它! – Crispin