Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Is mainloop() necessary?
#1
Henlo,
I'm a noob to Python and Tkinter, learning things, Using Python 3.7.4 in a Windows PC, using default IDLE for programming. This is a simple test program for example to my question.

-----------------------
#Example-1
from tkinter import *

def printer(event):
    print('Printing')

root = Tk()

frame1 = Frame(root)
frame1.pack()

button1 = Button(root, text='Button', command=printer)
button1.pack()

root.mainloop()
------------------------

question on Example-1:

If I keep or remove root.mainloop() results will be same, program will run indefinitely. If I keep, it will run without a >>> in the interactive shell of IDLE and on closing the window manually, >>> comes back. If I run it without .mainloop(), >>> comes immediately. On manual closing it another >>> comes on the shell. Button works all the time, until I close it manually. Not only Example-1, all programs works without .mainloop(). If I try to exit any program using a custom exit button/menu item, command line functions cease to respond but window remains open with all widgets on it. Same thing happens when I double click on .py file and let it run on windows command prompt. Is it normal? or a bug? or I'm doing something wrong?
Yoriz wrote Sep-01-2019, 02:18 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
IDLE is written in tkinter, running tkinter code in IDLE will use idle's mainloop.
Best advice is not to use IDLE for tkinter code.

Edit: yes mainloop is necessary.
Quote
#3
(Sep-01-2019, 02:35 PM)Yoriz Wrote: IDLE is written in tkinter, running tkinter code in IDLE will use idle's mainloop.
Best advice is not to use IDLE for tkinter code.

Edit: yes mainloop is necessary.

Thank you .....
Thank you .....
Thank you .....
Thank you .....
Thank you .....
Dance

Thank you for arranging my first post and sending 'How To Arrange' link.....

Thank you for solving the problem .....

It worked!

I hadn't any idea that IDLE is written in tkinter and codes are using it's mainloop().

Thanks Man!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] sleep(n) not working inside tkinter mainloop roger31415 2 591 Jul-14-2019, 06:57 PM
Last Post: wuf
  [split] What is a GUI's mainloop ? kom2 1 412 May-04-2019, 01:58 PM
Last Post: Yoriz
  [Tkinter] ttk.barprogress - root.mainloop francisco_neves2020 11 1,070 Apr-10-2019, 01:06 PM
Last Post: francisco_neves2020
  [Tkinter] How to update the gui in a loop without blocking the gui mainloop Qnuba 2 34,544 Nov-12-2016, 04:33 PM
Last Post: Qnuba

Forum Jump:


Users browsing this thread: 1 Guest(s)