【问题标题】:Blocking button click signals in PyQt在 PyQt 中阻止按钮单击信号
【发布时间】:2017-01-23 19:22:08
【问题描述】:

我有一个程序,它使用 pyqt 的 .animateClick() 功能向用户显示一系列不同的按钮单击,用户必须按特定顺序复制这些按钮。问题是我不希望 animateClick() 发送信号,我只希望来自用户的按钮单击信号。这是我的一些代码来演示我的意思,以及我如何尝试解决该问题(这不起作用)。我简化了我的代码,使其更易于阅读,如果您有任何问题,请告诉我。

from PyQt4 import QtCore,QtGui
global flag
global ai_states
ai_states = []
user_states = []


class Program(object):
    # Set up the push buttons
    #Code Here.

    # Connect push buttons to function get_state()
    self.pushButton.clicked.connect(self.get_state)
    self.pushButton_2.clicked.connect(self.get_state)
    self.pushButton_3.clicked.connect(self.get_state)        
    self.pushButton_4.clicked.connect(self.get_state)

    # Code that starts the start() function

    def start(self):
        flag = 0
        ai_states[:] = []
        i = -1
        # Code here that generates ai_states, numbers 1-4, in any order, based on button numbers.

        for k in ai_states:
            i = i + 1
            # Code here that animates button clicks determined by ai_states

            # Changes the flag to 1 once the loop ends
            if i == len(ai_states):
                flag = 1

    def get_state(self):
        if flag == 1:
            user_states.append(str(self.centralWidget.sender().text()))
        else:
            pass

        if len(user_states) == len(ai_states):
            # Checks to make sure the user inputted the same clicks as the ai_states

即使在 start() 函数之后标志确实为 1,它仍然会附加 animatedClick() 信号。我究竟做错了什么?我是 GUI 编程的新手,所以我可能会以一种非常糟糕的方式来解决这个问题。任何帮助将不胜感激。

【问题讨论】:

    标签: python user-interface pyqt signals-slots


    【解决方案1】:

    除非确实需要,否则切勿使用全局变量。如果您需要对变量进行共享访问,请使用实例属性:

    from PyQt4 import QtCore,QtGui   
    
    class Program(object):
        def __init__(self):
            self.ai_states = []
            self.user_states = []
            self.flag = 1
    
            # Set up the push buttons
            # Code Here
    
            # Connect push buttons to function get_state()
            self.pushButton.clicked.connect(self.get_state)
            self.pushButton_2.clicked.connect(self.get_state)
            self.pushButton_3.clicked.connect(self.get_state)        
            self.pushButton_4.clicked.connect(self.get_state)
    
        # Code that starts the start() function
    
        def start(self):
            self.flag = 0
            del self.ai_states[:]
            i = -1
            # Code here that generates ai_states, numbers 1-4, in any order, based on button numbers.
    
            for k in self.ai_states:
                i = i + 1
                # Code here that animates button clicks determined by ai_states
    
            # Changes the flag to 1 once the loop ends
            self.flag = 1
    
        def get_state(self):
            if self.flag == 1:
                self.user_states.append(str(self.centralWidget.sender().text()))
    
            if len(self.user_states) == len(self.ai_states):
                # Checks to make sure the user inputted the same clicks as the ai_states
    

    【讨论】:

      猜你喜欢
      • 2022-10-04
      • 2020-12-28
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2021-12-18
      相关资源
      最近更新 更多