-1

I'm trying to make a simple GUI. The iron, gold, diamond, and emerald variables increase at specific times. I want to have my while loop going while my tkinter main loop goes. Any help will be appreciated. Thanks!

I got to this but I the iron label won't update.

import tkinter as tk

second=0
diamonds=0

#Team One Variables
i=0
g=0
d=0
e=0

teamOneHasEmeralds=False

#Team Two Variables
i2=0
g2=0
d2=0
e2=0

teamTwoHasEmeralds=False

#Setting Up Screen
root=tk.Tk()
root.title("Bedwars")
#screen = tk.Frame(root)
#screen.pack()

#Labels
iron = tk.Label(root, text=i)

def main():
    global i
    global i2
    global second
    i+=1
    i2+=1
    second+=1
    print("Team One Iron=",i)
    print("Team Two Iron=",i2)
    root.after(1000, main)
    iron.pack()
    root.mainloop()
    if second%3 == 0:
        global g
        global g2
        g+=1
        g2+=1
        print("Team One Gold=",g)
        print("Team Two Gold=",g2)
    if second%20==0:
        global diamonds
        if diamonds < 5:
            diamonds+=1
        print("Diamonds=",diamonds)
    if teamOneHasEmeralds and second%25==0:
        global e
        e+=1
        print("Team One Emeralds=",e)
    if teamTwoHasEmeralds and second%25==0:
        global e2
        e2+=1
        print("Team Two Emeralds=",e2)

root.after(1000, main)
root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Possible duplicate of [How do you run your own code alongside Tkinter's event loop?](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) – user10455554 Oct 12 '18 at 23:35
  • I got to this but I the iron label won't update. – MrBlueMoose Oct 13 '18 at 04:47

1 Answers1

1

Now the issue is just updating the text-value of your label. To do so, you don't have to call iron.pack() repeatedly but just once (e.g. in your case before your main). Within your function update the text-property of the iron-label iron["text"] = i

Also you don't need to call root.mainloop() multiple times.

user10455554
  • 403
  • 7
  • 14