0

I think the problem is that the timer loop is only happening once. I am trying to loop the score and present it on the second screen, replacing the old score with the new one. The screen presents what the loop goes through, but the loop only happens once. The main goal of the program is to create 2 screens. The first screen asks for inputting initials. The second screen will pop up when the first screen hits "Start Game". The second screen will then show the initials and the score. The problem is the score will only loop one time.

Here is the code:

import tkinter as tk
from time import sleep
import addscore as a
import numpy as np
#from addscore import AddScore as add
#from addscore import SubstractScore as subtract
#from addscore import GameOver as over
LARGE_FONT=("Verdana", 20)

class App():

    def __init__(self):

        def RunningScore():
            global scoreObject
            scoreObject=tk.StringVar()
            score=0
            scoreObject.set(999)
            initial=entry.get()
            global top
            top=tk.Toplevel(bg='red')
            top.geometry('600x800')
            frame = tk.Frame(top, bg='gray', bd=5)
            frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.6, anchor='n')
            lbl=tk.Label(frame,textvariable=scoreObject, font=LARGE_FONT)
            lbl.place(relx=.47,rely=.5)
            lbl3=tk.Label(frame,text=score, font=LARGE_FONT)
            lbl3.place(relx=.47,rely=.7)

            lbl2=tk.Label(top,text=initial, font=LARGE_FONT)
            lbl2.place(relx=.45,rely=.1)
            top.after(50,lambda:UpdateScore(lbl3))
            frame.tkraise()
            top.mainloop()
        def UpdateScore(lbl3):
            score1=int(scoreObject.get())
            print(f'Before call score: {scoreObject.get()}')
            print(f'inside update: {score1}')
            #score1=a.AddScore(score1)
            score1=score1+5
            scoreObject.set(score1)
            lbl3['text']=score1
            print(f'After call score: {scoreObject.get()}')


        def LeaderBoard():
            top=tk.Toplevel(bg='blue')
            top.geometry('500x500')
            lbl=tk.Label(top,text="LeaderBoard")
            lbl.place(relx=.5,rely=.5)
            top.after(5000,lambda:top.destroy())

        root=tk.Tk()
        canvas=tk.Canvas(root, height=600, width=800)
        canvas.pack()
        background_image= tk.PhotoImage(file='ncstate.gif')
        background_label = tk.Label(root, image=background_image)
        background_label.place(relwidth=1, relheight=1)

        frame = tk.Frame(root, bg='gray', bd=5)
        frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.2, anchor='n')

        entry="Input Initials"
        entry=tk.Entry(frame,textvariable=entry, font=40)
        entry.place(rely=0.33, relwidth=0.4, relheight=.5, relx=.2)

        inputinitial=tk.Label(frame, text='Input Initials')
        inputinitial.place(relx=0.03, relheight=.5, rely=0.33)


        startbutton=tk.Button(frame, text="Start Game", command= lambda: RunningScore())
        startbutton.place(relx=0.67, rely=0.33, relwidth=.3, relheight=.5)

        lbbutton=tk.Button(root, text="Show Leader Board", command=LeaderBoard)
        lbbutton.place(rely=.75, relx=.37, relwidth=.25, relheight=.2)


    def quit(self):
        self.root.destroy() 

app = App()
app.mainloop() 
  • `top.mainloop()` is blocking. Read [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Nov 25 '19 at 15:02
  • you should use only one `mainloop()` but I see two `mainloop()` in your code. – furas Nov 25 '19 at 15:41
  • you should learn how to use `self.` instead of `global` – furas Nov 25 '19 at 15:43
  • in GUI frameworks (not only in tkinter)` you shouldn't use sleep or any long running code because it may block `mainloop()` which gets key/mouse events from system, send them to widgets and redraw widgets and window. When mainloop is blocked then program freeze. You should use `root.after(milliseconds, function_name)` to execute function periodically (without `for`-loop) and it will not block `mainloop()` – furas Nov 25 '19 at 15:46
  • When you call `after`, it's only designed to run the function once. – Bryan Oakley Nov 25 '19 at 15:56

0 Answers0