初学pyqt5,做一个小程序练手。具体问题是,我做了一个窗口,在这个窗口里,选择同意会弹出另一个窗口,但另一窗口总是一闪而过。求教。
![](http://tiebapic.baidu.com/forum/w%3D580/sign=3338eb50af99a9013b355b3e2d950a58/fd510ef531adcbef5a014942bbaf2edda2cc9fb4.jpg?tbpicau=2025-02-26-05_af98793adecb78158577239c72944011)
具体代码如下:
from random import *
from PyQt5.QtWidgets import *
import sy
class Child_old_jiyv(QWidget):
def __init__(self):
#print(1)
super().__init__()
self.old_jiyv()
def old_jiyv(self):
button_y=QPushButton("同意")
button_n = QPushButton("拒绝")
info=QLabel()
info.setText("你遇到了一个衣衫褴褛的老人,他向你要一些木材!")
hbox=QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(button_y)
hbox.addWidget(button_n)
vbox=QVBoxLayout()
vbox.addStretch(1)
vbox.addWidget(info)
vbox.addLayout(hbox)
self.setLayout(vbox)
button_y.clicked.connect(lambda:self.yes())
button_n.clicked.connect(qApp.quit)
self.setGeometry(300,300,300,50)
self.setWindowTitle("提示")
self.show()
def yes(self):
info_text_shit=["老人很感激你!","老人决定教你一项绝世神功!"]
info_text=choice(info_text_shit)
if info_text=="老人很感激你!":
print("老人很感激你!")
info_window=QWidget()
info_window.setWindowTitle("提示")
info_window.resize(300,50)
info_window.move(300,300)
info = QLabel()
info.setText(info_text)
yes_hbox = QHBoxLayout()
yes_hbox.addStretch(1)
yes_hbox.addWidget(info)
yes_vbox = QVBoxLayout()
yes_vbox.addStretch(1)
yes_vbox.addWidget(info)
yes_vbox.addLayout(yes_hbox)
info_window.setLayout(yes_vbox)
info_window.show()
else:
print("老人决定教你一项绝世神功!")
info_window = QWidget()
info_window.setWindowTitle("提示")
info_window.resize(300, 50)
info_window.move(300, 300)
yes_info = QLabel()
yes_info.setText("你的木材-1"+"\n"+info_text)
yes_hbox = QHBoxLayout()
yes_hbox.addStretch(1)
yes_hbox.addWidget(yes_info)
yes_vbox = QVBoxLayout()
yes_vbox.addStretch(1)
yes_vbox.addWidget(yes_info)
yes_vbox.addLayout(yes_hbox)
info_window.setLayout(yes_vbox)
info_window.setGeometry(300, 300, 300, 50)
info_window.setWindowTitle("提示")
info_window.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
C=Child_old_jiyv()
C.old_jiyv()
C.yes()
sys.exit(app.exec_())
![](http://tiebapic.baidu.com/forum/w%3D580/sign=3338eb50af99a9013b355b3e2d950a58/fd510ef531adcbef5a014942bbaf2edda2cc9fb4.jpg?tbpicau=2025-02-26-05_af98793adecb78158577239c72944011)
具体代码如下:
from random import *
from PyQt5.QtWidgets import *
import sy
class Child_old_jiyv(QWidget):
def __init__(self):
#print(1)
super().__init__()
self.old_jiyv()
def old_jiyv(self):
button_y=QPushButton("同意")
button_n = QPushButton("拒绝")
info=QLabel()
info.setText("你遇到了一个衣衫褴褛的老人,他向你要一些木材!")
hbox=QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(button_y)
hbox.addWidget(button_n)
vbox=QVBoxLayout()
vbox.addStretch(1)
vbox.addWidget(info)
vbox.addLayout(hbox)
self.setLayout(vbox)
button_y.clicked.connect(lambda:self.yes())
button_n.clicked.connect(qApp.quit)
self.setGeometry(300,300,300,50)
self.setWindowTitle("提示")
self.show()
def yes(self):
info_text_shit=["老人很感激你!","老人决定教你一项绝世神功!"]
info_text=choice(info_text_shit)
if info_text=="老人很感激你!":
print("老人很感激你!")
info_window=QWidget()
info_window.setWindowTitle("提示")
info_window.resize(300,50)
info_window.move(300,300)
info = QLabel()
info.setText(info_text)
yes_hbox = QHBoxLayout()
yes_hbox.addStretch(1)
yes_hbox.addWidget(info)
yes_vbox = QVBoxLayout()
yes_vbox.addStretch(1)
yes_vbox.addWidget(info)
yes_vbox.addLayout(yes_hbox)
info_window.setLayout(yes_vbox)
info_window.show()
else:
print("老人决定教你一项绝世神功!")
info_window = QWidget()
info_window.setWindowTitle("提示")
info_window.resize(300, 50)
info_window.move(300, 300)
yes_info = QLabel()
yes_info.setText("你的木材-1"+"\n"+info_text)
yes_hbox = QHBoxLayout()
yes_hbox.addStretch(1)
yes_hbox.addWidget(yes_info)
yes_vbox = QVBoxLayout()
yes_vbox.addStretch(1)
yes_vbox.addWidget(yes_info)
yes_vbox.addLayout(yes_hbox)
info_window.setLayout(yes_vbox)
info_window.setGeometry(300, 300, 300, 50)
info_window.setWindowTitle("提示")
info_window.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
C=Child_old_jiyv()
C.old_jiyv()
C.yes()
sys.exit(app.exec_())