Home > other >  Why is my tkinter loop not modifying variable?
Why is my tkinter loop not modifying variable?

Time:02-28

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()

CodePudding user response:

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

  • Related