0

I'm new to python and I am trying to create a program but I can't even get the basics right. I have a button app that looks like this:

#simple GUI
from tkinter import *
import time

#create the window
root = Tk()

#modify root window
root.title("Button Example")
root.geometry("200x50")

button1state = 0

def start():
    count = 0
    button1["text"] ="Busy!"
    while (count < 5):
        root.after(1000)
        count = count + 1

def button1clicked():
    global button1state
    if button1state == 0:
        start()
        button1["text"] ="On!"
        button1state = 1
    else:
        button1["text"] ="Off!"
        button1state = 0

app = Frame(root)
app.pack()

button1 = Button(app, text ="Off!", command = button1clicked)
button1.pack()

#kick off the event loop
root.mainloop()

Now everything works except it doesn't change the button text to busy while **start()** is called. How can I fix this? Once I've got it working I want to use images to show the user that its OFF ON and BUSY. Please help me

2 Answers 2

2

You need to force the GUI to update before starting the task:

def start():
    count = 0
    button1.configure(text="Busy!")
    root.update()  # <-- update window
    while (count < 5):
        root.after(1000)
        count = count + 1

But if you don't want your GUI to be frozen while the task is executed, you will need to use a thread as Dedi suggested.

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

3 Comments

I agree but you might need one if you want to perform some more complex task.
the same way: window_name.update(), where window_name can be something like self.window or just self if your class is inheriting from a tkinter widget.
Thank you so much. Works like a dream now. Where do I post my script to get help on checking it for any uneeded code?
0

You have to make a thread in order to make you function as a "background event" while your interface is working. Consider using that :

from threading import Thread

and then :

my_thread=Thread(target=start())
my_thread.start()

Where the first "start()" is the name of your function and the second one a call for the thread to begin.

Comments

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.