我正在尝试围绕我已经拥有的一些代码构建GUI。我知道如何在手动构建GUI时执行此操作,但在将此添加到Qt Designer和pyuic生成的python代码时会遇到困难。作为一个例子,我可能需要一个允许用户指向文件的按钮,我手动操作,这样就可以了:
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
btn = QtGui.QPushButton('Open File', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
btn.clicked.connect(self.loadFile)
self.setGeometry(300, 300, 250, 150)
self.show()
def loadFile(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
# some custom code for reading file and storing it
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
但是,当我尝试在Qt Designer代码中执行相同操作时,程序会在到达文件对话框之前停止。
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(130, 100, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.loadFile)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.pushButton.setText(_translate("Form", "Open File", None))
def loadFile(self):
print('loadFile1')
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
print('loadFile2')
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
这只会打印loadFile()中的第一个语句,但不会打开文件对话框窗口。我做错了什么?
答案 0 :(得分:1)
QString getOpenFileName( QWidget parent =无 ,QString caption ='', QString directory ='',QString filter ='',Options options = 0)
QString getOpenFileName( QWidget parent =无 ,QString caption ='', QString directory ='',QString filter ='',QString selectedFilter = '',选项选项= 0)
你需要传递一个小部件或者无,在你的情况下,self不是object类型。
你必须改变
QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
到
QtGui.QFileDialog.getOpenFileName(None, 'Open file', '/home')
答案 1 :(得分:0)
我真的不喜欢使用pyuic,只要我能避免。您可以尝试一种更简单的方法,它可以减少您的代码。假设您的UI文件名为something.ui,并且您已在QT Designer中将按钮命名为somebutton,则代码将为:
from PyQt4 import QtCore, QtGui, uic
Ui_somewindow, _ = uic.loadUiType("something.ui") #the path to your UI
class SomeWindow(QtGui.QMainWindow, Ui_somewindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_somewindow.__init__(self)
self.setupUi(self)
self.somebutton.clicked.connect(self.loadFile)
def loadFile(self):
print('loadFile1')
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
print('loadFile2')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = SomeWindow()
window.show()
sys.exit(app.exec_())
请注意,如果你有QDialog用QDialog替换QMainWindow。 希望这会有所帮助。