Home > front end >  Tkinter : Window not opening when incrementing value,
Tkinter : Window not opening when incrementing value,

Time:02-14

I'm making a BSOD simulator with a randomly progressing percentage. What i'm trying to do is :

  1. Make a variable called percentage and initialise it with a value of 0
  2. Make a label with the text f"{percentage}% completed"
  3. Make a variable called percentageIsAHundred and initialise it with a value of False

Works good. Output : 0% completed

Now to add the random steps :

  1. Make a loop as long as percentageIsAHundred is false
  2. Change the value of percentageText to f"{percentage}% completed" (to update it)
  3. We do a win.after() (I've tried sleep and threading- don't work) and put the values 1000, and a function called steps
  4. Check if the percentage value is above 100, If it returns true we set percentageText to 100% completed (otherwise it might be 106% or 104%) and set percentageIsAHundred to true, breaking the loop.

In the steps function we:

  1. Increment the percentage variable by a random value in between 0 and 20

Should work? Nope.

No windows open and no errors appear in console. Commenting out the code works and returns to normal.

I have seen that this behaviour occurs at the steps() function, Although I may be wrong.

# Importing modules
from tkinter import *
import os
import random
import threading

# Initialising Tkinter window
win = Tk()

# Creating reboot function
def reboot(sec=50):
    os.system(f'echo "{sec} timer not confirmed"')


# Giving the window its iconic blue background.
win.config(bg="#0178d6")

# Making the window fullscreen
win.attributes("-fullscreen", True)

# Adding the emoticon ":("
emoticon = Label(win, text=":(", font="{Segoe UI} 100", bg="#0178d6", fg="#ffffff")
emoticon.place(x="50px", y="100px")

# Adding the text
text1 = Label(win, text="A problem has occurred and your PC needs to restart. We're just collecting some error information, then we'll restart for you.", font="{Segoe UI} 20", bg="#0178d6", fg="#ffffff")
text1.place(x="50px", y="259px")

# Adding the progress percentage
percentage = 0
percentageText = Label(win, text=f"{percentage}% complete", font="{Segoe UI} 20", bg="#0178d6", fg="#ffffff")
percentageText.place(x="50px", y="320px")
percentageIsAHundred = False

________________________________________________________

# This is the problematic code- Removing it seems to run the window normally but including it causes tkinter to not run with 0 errors  

def steps(percentage=percentage) :
    percentage = percentage   random.randint(1, 20)
win.after(1000, steps)
while percentageIsAHundred == False :
    percentageText.config(text=f"{percentage}% complete")
    if percentage >= 100 :
        percentageText.config(text="100% complete")
        percentageIsAHundred = True
________________________________________________________

# Setting the mainloop
win.mainloop()

! My title may not be right because I can't think clear right now. !

CodePudding user response:

The percentage in steps() is local to that function.

If you want it to increase the value of the global percentage, you should mark it at such:

def steps():
    global percentage
    percentage  = random.randint(1, 20)
    win.after(1000, steps)
    percentageText.config(text=f"{percentage}% complete")
    if percentage >= 100:
        percentageText.config(text="100% complete")
        win.quit()

Also note that as a rule you should be careful with while and for loops in event handlers and after functions. If they take too long, they will inhibit event processing.

Also, your while loop is not indented, so it in not part of steps but part of the main module. In this case, since your your while loop runs before the mainloop, it prevents the tkinter mainloop from starting and your window from appearing. :-)

  • Related