2015-11-07 53 views
0

我正在做一个简单的基于文本的冒险使用PyQt和按钮。出于某种原因,点击几个按钮后,在刷新屏幕之前单击按钮时会出现一些可怕的延迟。为什么会发生?因为我有没有知道发生了什么导致这个问题,我会复制整个〜100行程序...为什么按下几个按钮后我的应用程序会滞后?

import sys, time 
from PyQt4 import QtGui, QtCore 

class Window(QtGui.QMainWindow): 

    def __init__(self): 
     super(Window, self).__init__() 
     self.setGeometry(50, 50, 600, 400) 
     self.setWindowTitle("Omar OK Adventures!") 

     QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("plastique")) 

     self.home() 

    def home(self): 
     btn1 = QtGui.QPushButton("New Game", self) 
     btn1.clicked.connect(self.change) 
     btn1.resize(500, 25) 
     btn1.move(50, 275) 
     self.btn1 = btn1 

     btn2 = QtGui.QPushButton("What's this all about?", self) 
     btn2.clicked.connect(self.wat) 
     btn2.resize(500, 25) 
     btn2.move(50, 305) 
     self.btn2 = btn2 

     btn3 = QtGui.QPushButton("...", self) 
     btn3.resize(500, 25) 
     btn3.move(50, -200) 
     self.btn3 = btn3 

     btn4 = QtGui.QPushButton("...", self) 
     btn4.resize(500, 25) 
     btn4.move(50, -200) 
     self.btn4 = btn4 

     txt = "Welcome to the main menu! (WIP obviously)" 
     lbl = QtGui.QLabel(txt, self) 
     lbl.resize(500, 200) 
     lbl.move(50, 25) 
     lbl.setAlignment(QtCore.Qt.AlignCenter) 
     self.lbl = lbl 
     self.show() 

    def homefake(self): 
     self.btn1.setText("Begin teh epic adventures") 
     self.btn1.clicked.connect(self.change) 
     self.btn2.setText("What's this all about?") 
     self.btn2.clicked.connect(self.wat) 
     self.btn2.move(50,305) 
     self.btn3.move(50, -200) 
     self.btn4.move(50, -200) 
     self.lbl.setText("Welcome to the main menu! (WIP obviously)") 

    def close_application(self): 
     choice = QtGui.QMessageBox.question(self, "Gemme outta here!", 
              "Do you really want to quit?", 
              QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) 
     if choice == QtGui.QMessageBox.Yes: 
      sys.exit() 
     else: 
      pass 

    def closeEvent(self, event): 
     event.ignore() 
     self.close_application() 

    def C1_1(self): 
     self.lbl.setText("You tell your parents that you're going to Japan to get some 'stuff'\nThey tell you to be back at dinner\nOk.\nYou buy a plane ticket and board the plane\nYou realize boarding the plane is boring as hell.\nWhat should you do to kill time?") 
     self.btn1.setText("Take over the plane (Try not to crash it horribly)") 
     self.btn1.clicked.connect(self.C2_1) 
     self.btn2.setText("Watch a movie on the not-tablet stuck on a chair infront of you (Really, it's a tablet stuck on a chair.)") 
     self.btn2.move(50, 305) 
     self.btn2.clicked.connect(self.C2_2) 
     self.btn3.move(50, -200) 
     self.btn4.move(50, -200) 

    def C2_1(self): 
     self.btn1.setText("Restart game") 
     self.btn1.clicked.connect(self.homefake) 
     self.btn2.move(50, -200) 
     self.lbl.setText("You pull out your combat shotgun and tell everyone to freeze and go on the floor.\nNeedless to say, people are screaming.\nSome guy tried to tackle you\nYou shot him\nSome air police jackass lands a headshot on you while you were looking away\nYou died.\n\nGood job.") 

    def C2_2(self): 
     self.lbl.setText('lol') 

    def C1_2(self): 
     self.lbl.setText("ARMED MEN") 

    def C1_3(self): 
     self.lbl.setText("TAKIN A HIKE") 

    def C1_4(self): 
     self.lbl.setText("HORSE TIME") 

    def wat(self): 
     self.lbl.setText("What's this?\nThis is pretty much a 5 minute (or so) test to see the capabilities of text based adventures.\n I'll probably be doing stuff much better later on. Keep an eye out!\nI probably won't replace this even if it's a joke program. Just because.\n\nCreated by popcar2") 
     self.btn1.setText("Back to main menu") 
     self.btn2.move(50, -200) 
     self.btn1.clicked.connect(self.homefake) 

    def change(self, txt): 
     self.lbl.setText("You're sitting at home, incredibly bored as usual.\nHowever... You decided to do something new in your okeil life...\nA few ideas pop up in your head, what do you wanna do?") 
     self.btn1.setText("Go steal whatever's nuclear in one of Japan's nuclear power plants (Diseases might be included)") 
     self.btn2.move(50, 305) 
     self.btn2.setText("Hire a group of armed men and take over the school (Be sure to tie kids as hostages)") 
     self.btn3.move(50, 335) 
     self.btn3.setText("Take a walk (TAKE A HIKE, MATE)") 
     self.btn4.move(50, 365) 
     self.btn4.setText("Steal a horse (The only logical option)") 
     self.btn1.clicked.connect(self.C1_1) 
     self.btn2.clicked.connect(self.C1_2) 
     self.btn3.clicked.connect(self.C1_3) 
     self.btn4.clicked.connect(self.C1_4) 
def run(): 
    app = QtGui.QApplication(sys.argv) 
    GUI = Window() 
    sys.exit(app.exec_()) 
run() 

回答

1

当你犯了一个信号连接(btnX.clicked.connect()),这并不自动断开之前连接的插槽。因此,当你通过游戏进行游戏时,每个按钮点击实际上正在执行每个连接到它的方法,随着越来越多的方法连接到每个按钮的点击信号,开始变得越来越慢。

在与btnX.clicked.connect(new_slot)建立新连接之前,您需要先致电btnX.clicked.disconnect(previous_slot)

这要求您在连接下一个方法时知道以前连接了哪个插槽(因为您需要引用方法名称)。我没有按照你的程序的逻辑来看看它是多么容易,但这是你需要做的。否则,您需要采取不同的方法,不需要更改按钮所连接的插槽(例如,使用堆叠小工具在不同组按钮之间切换或类似)

+0

您能否提供一个示例?我试过在点击它后断开更改功能,但它返回了这个错误: self.btn1.clicked.disconnect(self.change) TypeError:disconnect()在'clicked'和'unislot'之间失败 –

+0

@PopCar你可能想看看[this](http://stackoverflow.com/q/21586643/1994235)stackoverflow问题/答案。我怀疑当您尝试断开当时并未实际连接的方法时发生错误。这可能表明程序正在以未预测的方式在各个方法之间移动,因此您必须认真执行程序执行以找出当时“更改”方法未连接的原因。尝试在每次“连接”和“断开连接”调用后添加打印语句以查看发生了什么 –

+0

我已经想出了答案。看来,btnx.clicked.disconnect()函数不需要参数。这意味着我可以直接输入“btnx.clicked.disconnect()”,它将断开它当前持有的任何东西。不需要名称等。谢谢! –

相关问题