4
我有點新手這麼善良;-)PyQt4的文件名對話框
我有一個GUI,我提出用PyQt4的和Python 2.6與工作文件對話框,(即你按下一個按鈕和一個窗口彈出並允許你選擇一個文件來加載/保存)。 GUI的代碼就像2000行,所以我將包括我認爲很重要的位:
from PyQt4 import QtGui as qt
from PyQt4 import QtCore as qc
class NuclearMotion(qt.QWidget):
def __init__(self, parent=None):
super(NuclearMotion, self).__init__(parent)
file_button = qt.QPushButton("Use data from file")
mainLayout = qt.QGridLayout()
mainLayout.addWidget(file_button, 14, 8, 1, 2)
def choose_file():
file_name = qt.QFileDialog.getOpenFileName(self, "Open Data File", "", "CSV data files (*.csv)")
self.connect(file_button, qc.SIGNAL("clicked()"), choose_file)
self.setLayout(mainLayout)
if __name__ == '__main__':
import sys
app = qt.QApplication(sys.argv)
NuclearMotionWidget = NuclearMotion()
NuclearMotionWidget.show()
sys.exit(app.exec_())
上面的工作絕對沒問題。我使用各種教程手動輸入了所有代碼。我現在已經使用QT設計器和pyuic4將一個新的GUI轉換爲一個.py文件。現在我無法使文件對話框工作。下面的代碼導致錯誤類型:
from PyQt4 import QtCore, QtGui
class Ui_mainLayout(object):
def setupUi(self, mainLayout):
mainLayout.setObjectName(_fromUtf8("mainLayout"))
mainLayout.resize(598, 335)
mainLayout.setTabPosition(QtGui.QTabWidget.North)
mainLayout.setTabShape(QtGui.QTabWidget.Rounded)
mainLayout.setElideMode(QtCore.Qt.ElideLeft)
self.basic_tab = QtGui.QWidget()
self.file_button = QtGui.QPushButton(self.basic_tab)
QtCore.QObject.connect(self.file_button, QtCore.SIGNAL("clicked()"), self.choose_file)
def choose_file(self):
file_name = QtGui.QFileDialog.getOpenFileName(self, "Open Data File", "", "CSV data files (*.csv)")
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
mainLayout = QtGui.QTabWidget()
ui = Ui_mainLayout()
ui.setupUi(mainLayout)
mainLayout.show()
sys.exit(app.exec_())
這段代碼產生的GUI只是罰款和其他一切工作正常,包括信號。任何想法我做錯了什麼!?
所以如果我用(qt.QWidget)替換(對象)它應該工作?或者我需要添加它像(object,qt.QWidget)我會嘗試它,但我遠離我的電腦!感謝您的快速反應btw – Ben 2011-06-16 12:49:00
我試了一下,改變對象到QWidget工作完美! – Ben 2011-06-16 15:36:52