0

I wanted to know about the optional integer parameter for the mainloop() function. There is no documentation about this on python.org nor youtube.


The First Code: passing no parameters in mainloop

from tkinter import *

def fun():
    global i
    print("hello", i)
    i += 1
    root.mainloop()
    print("Hi", i)
    return

i = 0
root = Tk()
button1 = Button(root, text="click", command=fun)
button1.pack()
root.mainloop()

print("end")

Output: clicked button 3 times and closed window

hello 0
hello 1
hello 2
Hi 3
Hi 3
Hi 3
end

The Second code: passing i in mainloop

from tkinter import *

def fun():
    global i
    print("hello", i)
    i += 1
    root.mainloop(i)
    print("Hi", i)
    return

i = 0
root = Tk()
button1 = Button(root, text="click", command=fun)
button1.pack()
root.mainloop(i)

print("end")

Output: clicked button 3 times and closed window

hello 0
Hi 1
hello 1
Hi 2
hello 2
Hi 3
end

the codes without parameters are working in a suspicious manner which I believe is could be due to recursion.

Groudon
  • 26
  • 3
  • Unless you have a deep understanding of how tk works at the C level, you should ignore this parameter. However, you can see precisely how its used by looking at the c code here: https://github.com/python/cpython/blob/master/Modules/_tkinter.c – Bryan Oakley Oct 02 '19 at 13:16
  • Understanding tkinter mainloop https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop – Kenly Oct 02 '19 at 13:20
  • 1
    Possible duplicate of [What is the n parameter of tkinter.mainloop function?](https://stackoverflow.com/questions/51386979/what-is-the-n-parameter-of-tkinter-mainloop-function) – carlesgg97 Oct 02 '19 at 13:25

0 Answers0