Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my tkinter loop not modifying variable?

I am a beginner and experimented with tkinter (in Python) for a project. I am trying to let a loop pack numbers in a window but I yust cannot get it to work. It shuld count up from 0 but it packs only 0.
Would be great if someone could help!
Philipp

from tkinter import *

window = Tk()
window.title("window")
window.resizable(False, False)
window.geometry("500x500")
window.configure(background="white")

i = 0
while i < 100:
    text = 0
    label = Label(window, text=text)
    label.pack()
    print(text)
    text += 1
    i += 1

window.mainloop()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You set text to 0 at each iteration:

from tkinter import *

window = Tk()
window.title("window")
window.resizable(False, False)
window.geometry("500x500")
window.configure(background="white")

i = 0
text = 0  # <- MOVE HERE
while i < 100:
    label = Label(window, text=text)
    label.pack()
    print(text)
    text += 1
    i += 1

window.mainloop()

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading