jeudi 23 juin 2016

How can I make a script wait for a button press?


I apologize if the answer to this question is obvious, but I am unfamiliar with the inner workings of Python.

Basic Scenario: I have a Python script that does stuff. Periodically, when certain conditions are met (like a flag variable being set), I want the script to pause and wait for the user to press a GUI button.

How can I implement this?

To clarify, I want it to work like input() does, in that it pauses everything and waits for the function call to be resolved, except that I don't want it tied to text, but rather a GUI button. I plan on using Tkinter to make the button.

My initial thoughts are making a while loop something like this:

x = 2
while (x > 1):
    #do nothing

And then the button will call a function that sets x = 0

Is this the proper way to do this? Is there a better way? Am i missing something obvious?

Sample Code:

class Displayable(object):
    max_display_level = 1   # can be overridden in subclasses
    manual_step = False     # can be overridden in subclasses

    def display(self,level,*args,**kwargs):
        -Do stuff unrelated to the question-

        if (self.manual_step):
           if level <= self.max_display_level:
               input("Waiting for input: ")

The idea behind this is objects will extend the class Displayable and set their own max_display_level and manual_step values. A higher max_display_level means more messages will be displayed (ie. if I call display() with levels = 1,2,3, and 4, if my max_display_level ==2, only the first 2 calls will have all the logic executed. It is a way to allow a user to set the level of verbosity of the execution. It's there for a reason which I won't get into, but it should stay.) If a specific object has manual_step == true and the level is satisfied, it should wait for user input when display() is called. The trick is I want it to wait for a button press instead of text+enter.


Aucun commentaire:

Enregistrer un commentaire