auden
auden

Reputation: 1157

What can I use instead of a while loop that will check something over and over?

I have a button whose function is

def callback2():
    callback()

The callback() function is

def callback():
    usein = None
    if inspect.stack()[1][3] == callback2:
        global inputText
        usein = inputText.get()
        return None
    while True: #freezes everything, because tkinter
        if usein:
            return usein

Now, the reason I have to do it like this is because other functions call callback() looking for the value inputted by the button, but I have to make them wait for the button to be pressed. But since I'm using tkinter, the while loop doesn't work - it just makes the GUI freeze. So what can I use instead? I've been working on this for days. I'd be glad to add any other parts of my code if needed.

Upvotes: 0

Views: 78

Answers (2)

BrockLee
BrockLee

Reputation: 981

Not a tkinter expert, but if you want to get some text input on a button click, the following may work.

def callback():
    usein = entry.get()
    # do whatever with usein

master = Tk()
entry = Entry(master)  # the text input
Button(master, text='Button', command=callback)

Upvotes: 1

Raghavendra Bableshwar
Raghavendra Bableshwar

Reputation: 211

isButtonClicked = false #a global variable
def callback2():
    isButtonClicked = true
    callback()
    isButtonClicked = false

One idea may be to use a global variable called isButtonClicked and assign a false value, and modify the other methods which call callback method like this:

def othermethod():
if isButtonClicked:
    callback()

But you've to make sure that the variables are thread-safe.

Upvotes: 1

Related Questions