在我使用QT Creator创建UI并将其成功转换为python之后,我的所有按钮似乎都不起作用。我不知道为什么。
我制作了一个简单的Qstack布局,共有2页,每个页面都有一个按钮可以转到另一页。这在QtCreator中有效,但是当我在终端中运行python脚本时,只有一个打开的页面带有一个不起作用的按钮。
这是python代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.stackedWidget = QtWidgets.QStackedWidget(self.centralWidget)
self.stackedWidget.setGeometry(QtCore.QRect(9, 19, 391, 211))
self.stackedWidget.setObjectName("stackedWidget")
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.pushButton = QtWidgets.QPushButton(self.page)
self.pushButton.setGeometry(QtCore.QRect(130, 120, 114, 32))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.page)
self.label.setGeometry(QtCore.QRect(50, 20, 59, 16))
self.label.setObjectName("label")
self.stackedWidget.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.label_2 = QtWidgets.QLabel(self.page_2)
self.label_2.setGeometry(QtCore.QRect(70, 70, 59, 16))
self.label_2.setObjectName("label_2")
self.pushButton_2 = QtWidgets.QPushButton(self.page_2)
self.pushButton_2.setGeometry(QtCore.QRect(190, 120, 114, 32))
self.pushButton_2.setObjectName("pushButton_2")
self.stackedWidget.addWidget(self.page_2)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 22))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(MainWindow)
self.statusBar.setObjectName("statusBar")
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
self.stackedWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.label.setText(_translate("MainWindow", "PAGE 1"))
self.label_2.setText(_translate("MainWindow", "PAG 2"))
self.pushButton_2.setText(_translate("MainWindow", "Button 2"))
self.pushButton.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我对Qt真的很陌生。并且我正在尝试为学校项目制作多页用户界面。任何人关于如何让我的按钮再次起作用的任何想法吗?
非常感谢!
编辑:ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QStackedWidget" name="stackedWidget">
<property name="geometry">
<rect>
<x>9</x>
<y>19</y>
<width>391</width>
<height>211</height>
</rect>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>130</x>
<y>120</y>
<width>114</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>20</y>
<width>59</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>PAGE 1</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="page_2">
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>70</x>
<y>70</y>
<width>59</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>PAG 2</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>190</x>
<y>120</y>
<width>114</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>Button 2</string>
</property>
</widget>
</widget>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>