我目前是初学者,我正在尝试制作GUI。但是,我到处搜索超过3-4天,但找不到合适的答案。我想做的是,单击“开始”按钮后,应将窗口更改为second_layout窗口,但它不起作用。我希望这也会对其他人有所帮助!非常感谢你!
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot, QRect
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QApplication, QMainWindow, QLabel, QWidget, QPushButton, QLineEdit, QInputDialog, QStackedLayout
from PyQt5.uic import loadUi
from PyQt5.QtGui import QFont, QPixmap
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Plant those trees")
self.first_layout()
self.stacked_layout = QStackedLayout()
self.stacked_layout.addWidget(self.layout_1)
self.central_widget = QWidget()
self.central_widget.setLayout(self.stacked_layout)
self.setCentralWidget(self.central_widget)
def first_layout(self):
self.layout_1 = QWidget()
#image
self.image = QLabel(self.layout_1)
self.image.setGeometry(QRect(0, -10, 781, 381))
self.image.setPixmap(QPixmap("3957f79ab9264dc.jpg"))
#label
self.label = QLabel(self.layout_1)
self.label.setText("Plant those trees")
self.label.setGeometry(QRect(180, 10, 591, 241))
font = QFont()
font.setFamily("Segoe Script")
font.setPointSize(28)
self.label.setFont(font)
self.label.setStyleSheet("color: white;")
#push button 1
self.pushbutton1 = QPushButton(self.layout_1)
self.pushbutton1.setText("Enter name")
self.pushbutton1.setGeometry(QRect(307, 180, 180, 30))
font = QFont()
font.setFamily("Segoe Script")
font.setPointSize(15)
self.pushbutton1.setFont(font)
self.pushbutton1.setStyleSheet("background-color: white;")
self.pushbutton1.clicked.connect(self.gettext)
#push button 2
self.pushbutton2 = QPushButton(self.layout_1)
self.pushbutton2.setText("Start")
self.pushbutton2.setGeometry(QRect(307, 240, 75, 30))
self.pushbutton2.setFont(font)
self.pushbutton2.setStyleSheet("background-color: white;")
self.pushbutton2.clicked.connect(self.on_pushButton_clicked)
#push button 3
self.pushbutton3 = QPushButton(self.layout_1)
self.pushbutton3.setText("Exit")
self.pushbutton3.setGeometry(QRect(407, 240, 75, 30))
self.pushbutton3.setFont(font)
self.pushbutton3.setStyleSheet("background-color: white;")
self.pushbutton3.clicked.connect(exit)
def on_pushButton_clicked(self):
self.second_layout()
self.stacked_layout.addWidget(self.second_layout)
self.stacked_layout.setCurrentIndex(1)
def gettext(self):
self.text, okPressed = QInputDialog.getText(self, "Enter your name","Your name:", QLineEdit.Normal, "")
def second_layout(self):
self.layout_2 = QWidget()
self.pushButton = QPushButton(self.layout_2)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = App()
main.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
尝试一下:
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSlot, QRect
from PyQt5.QtWidgets import (QHBoxLayout, QVBoxLayout, QApplication,
QMainWindow, QLabel, QWidget, QPushButton, QLineEdit,
QInputDialog, QStackedLayout)
from PyQt5.uic import loadUi
from PyQt5.QtGui import QFont, QPixmap
class App(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Plant those trees")
self.first_layout()
self.initUI()
# +++
def initUI(self):
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.stacked_layout = QStackedLayout(self.central_widget)
self.stacked_layout.addWidget(self.layout_1)
def first_layout(self):
self.layout_1 = QWidget()
#image
self.image = QLabel(self.layout_1)
self.image.setGeometry(QRect(0, -10, 781, 381))
# self.image.setPixmap(QPixmap("3957f79ab9264dc.jpg"))
self.image.setPixmap(QPixmap("E:/_Qt/img/qt-logo.png"))
#label
self.label = QLabel(self.layout_1)
self.label.setText("Plant those trees")
self.label.setGeometry(QRect(180, 10, 591, 241))
font = QFont()
font.setFamily("Segoe Script")
font.setPointSize(28)
self.label.setFont(font)
self.label.setStyleSheet("color: white;")
#push button 1
self.pushbutton1 = QPushButton(self.layout_1)
self.pushbutton1.setText("Enter name")
self.pushbutton1.setGeometry(QRect(307, 180, 180, 30))
font = QFont()
font.setFamily("Segoe Script")
font.setPointSize(15)
self.pushbutton1.setFont(font)
self.pushbutton1.setStyleSheet("background-color: white;")
self.pushbutton1.clicked.connect(self.gettext)
#push button 2
self.pushbutton2 = QPushButton(self.layout_1)
self.pushbutton2.setText("Start")
self.pushbutton2.setGeometry(QRect(307, 240, 75, 30))
self.pushbutton2.setFont(font)
self.pushbutton2.setStyleSheet("background-color: white;")
self.pushbutton2.clicked.connect(self.on_pushButton_clicked)
#push button 3
self.pushbutton3 = QPushButton(self.layout_1)
self.pushbutton3.setText("Exit")
self.pushbutton3.setGeometry(QRect(407, 240, 75, 30))
self.pushbutton3.setFont(font)
self.pushbutton3.setStyleSheet("background-color: white;")
self.pushbutton3.clicked.connect(exit)
def on_pushButton_clicked(self):
# self.second_layout()
self.stacked_layout.addWidget(self.second_layout())
self.stacked_layout.setCurrentIndex(1)
def gettext(self):
self.text, okPressed = QInputDialog.getText(self, "Enter your name","Your name:", QLineEdit.Normal, "")
def second_layout(self):
self.layout_2 = QWidget()
self.pushButton = QPushButton('click my', self.layout_2)
self.pushButton.clicked.connect(self.initUI) # +++
return self.pushButton
if __name__ == '__main__':
app = QApplication(sys.argv)
main = App()
main.setGeometry(QRect(500, 100, 700, 400))
main.show()
sys.exit(app.exec_())