1

First I created a window with some buttons and I defined their commands. It all works fine until I add the while loop to check if any button was pressed and then move to the next step. But then the window doesn't show up and the loop is running forever. I would also like to know if there is a better alternative to my code.

from tkinter import *

Round = 0

def blackC():
    global Round
    print ('0')
    x = 0
    Round += 1

def brownC():
    global Round
    print ('1')
    x = 1
    Round +=1

def redC():
    global Round
    print ('2')
    x = 2
    Round += 2

def win():
    window = Tk()
    window.geometry ('500x500')
    window.title('HELLO')
    blackB = Button(text = 'BLACK', command=blackC, width=7, height=3, bd=5)
    blackB.place(x=1, y=1)
    brownB = Button(text = 'BROWN', command=brownC, width=7, height=3, bd=5)
    brownB.place(x=86, y=1)
    redB = Button(text = 'RED', command=redC, width=7, height=3, bd=5)
    redB.place(x=172, y=1)
    window.mainloop()

while (Round == 0):
    win()
while (Round < 3):
    if (Round == 1):
        y = x * 10
        print ('y')
    elif (Round == 2):
        y += x
        print ('y')

1 Answer 1

1

I don't know what exactly you mean by moving to the next step, but you definitely misunderstand how tkninter works. You are missing the parenthesis in the mainloop window.mainloop(). And you don't want to call it in the cycle, because mainloop is a function which is cycle. So you just run it once a time a then it runs infinitely. So your code have to just run once a time function win().

from tkinter import *

Round=0

def button(type):
    global Round
    print (str(type))
    x = type
    Round += type

def win():
    window = Tk()
    window.geometry ('500x500')
    window.title('HELLO')
    blackB = Button(text = 'BLACK', command=lambda: button(0), width=7, height=3, bd=5)
    blackB.place(x=1, y=1)
    brownB = Button(text = 'BROWN', command=lambda: button(1), width=7, height=3, bd=5)
    brownB.place(x=86, y=1)
    redB = Button(text = 'RED', command=lambda: button(2), width=7, height=3, bd=5)
    redB.place(x=172, y=1)
    window.mainloop

win()

You have asked for better code, so I have rewritten your buttons func for a one, which just takes an parametr of type and call it as a lambda function (take a look: http://www.diveintopython.net/power_of_introspection/lambda_functions.html. For a larger projects it is better to have tkinter window as a class, but in this is it enough.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that, but it still doesn't quite solve my problem.
Can you please be specific what you really need to know?
I have a windoe witho some buttons, which can change the variable x and Round. Then I have a while loop, which checks if the value Round has reached specific number and would then go to the next step. But while loop doesn't run until I close the window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.