Tkinter gives you the event loop for free

In order to process events efficiently, GUIs employ an event loop. Event loops watch and wait for events, calling a piece of code each time an event occurs. If you think about the current TVN Game Show program, it already has a very basic event loop that waits for the host to press 1, 2, or 0. The program then calls some code before waiting again for another key-press event from the host. To implement this in code, you used a while loop:

number_asked = number_asked + 1 number correct = number correct + 1

In tkinker, you don't need to write a while loop like you did for your non-GUI program. In tkinter, call the mainloop () method instead:

Click on the ┬┐lose box -to terminate ihis application."

Import everything from the tkinter module.

Give the window a name.

pvovide window toordinates and sire values.

from tkinter import *

Create a tkinter application app = Tk()

app.title("Your tkinter application") app.geometry('450x100+200+100') app.mainloop()

Start the tkinter event loop.

These f ive lines of Python/tkinter Code pvoduCe this GUI-

To add a button to your application, use code like this, being sure to put these two lines of code before the call to mainloop ():

Add a button to The packO method links the window and give the newly created button b1 = Button(app, text b1.pack()

Add a button to The packO method links the window and give the newly created button it some text and a to the existing window. width value.

"The button's been added to the 6UI.

Was this article helpful?

0 0

Post a comment