简体   繁体   中英

Need help updating a tkinter function every minute

Problem:

I am trying to create a program using tkinter to show daily baseball games, start times and live scores. The problem is with the live scores because I cannot find out how to integrate a way to update the function (daily_games) that holds the current scores.

I have only been programming for a few months so my code might be all over the place... also sorry for the long post.

What I have tried so far: I have tried using after() and tkinters update() but I was unsure how to integrate it.

Code:

# when called this method populates Start page with links, times, scores, and game status
def daily_games(self):
    streams = stream_links.today_streams()
    row = 5
    for game in streams:
        team_1 = game[1]
        team_2 = game[2]

        logos = team_logos(team_1, team_2)
        logo = logos[0]
        logo2 = logos[1]

        # assigns team one logo
        t1_logo = tk.Label(self, image=logo)
        t1_logo.image = logo
        t1_logo.grid(column=1, row=row, sticky='NSEW')

        # assigns team two logo
        t2_logo = tk.Label(self, image=logo2)
        t2_logo.image = logo2
        t2_logo.grid(column=4, row=row, sticky='NSEW')

        # links to streams
        lbl = tk.Label(self, text=team_1 + " vs " + team_2, fg="blue", cursor="hand2", font=AVG_FONT)
        lbl.grid(column=3, row=row, sticky='NSEW')
        assign_link(lbl, game[0])

        # uses mlbgame package to get gametimes, status, and scores
        game_info = matchup_info(team_1)
        game_status_lbl = tk.Label(self, text=game_info[1], font=AVG_FONT)
        game_status_lbl.grid(column=5, row=row, sticky='NSEW')
        score_lbl = tk.Label(self, text=game_info[2] + " - " + game_info[3], font=AVG_FONT, padx=10, pady=10)
        score_lbl.grid(column=6, row=row, sticky='NSEW')
        game_start_lbl = tk.Label(self, text=game_info[0], font=AVG_FONT, padx=10, pady=10)
        game_start_lbl.grid(column=0, row=row, sticky='NSEW')

        row += 2

    return row


# method works with mlb_data to find info on game matchups and returns desired data
def matchup_info(team):
    # assigning start times/ status/ scores
    game_start = mlb_data(DATE, names_dict[team])[0]
    status = mlb_data(DATE, names_dict[team])[1]
    t1_score = str(mlb_data(DATE, names_dict[team])[2])
    t2_score = str(mlb_data(DATE, names_dict[team])[3])

    return game_start, status, t1_score, t2_score


class BaseballStreamsApp(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, "C:\\Users\\\\PycharmProjects\\baseball_streams\\bbstreams\\mlb_logos\\MLB.ico")
        tk.Tk.wm_title(self, "MLB Streams")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand= True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = StartPage(container, self)

        self.frames[StartPage] = frame

        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        title_lbl = tk.Label(self, text="MLB Streams " + str(date.today()), font=LARGE_FONT)
        time_lbl = tk.Label(self, text="Time", font=AVG_FONT)
        game_lbl = tk.Label(self, text="Game", font=AVG_FONT)
        score_lbl = tk.Label(self, text="Score", font=AVG_FONT)
        status_lbl = tk.Label(self, text="Status", font=AVG_FONT)

        title_lbl.grid(column=2, row=0, columnspan=2, pady=10, padx=10, sticky='NSEW')
        time_lbl.grid(column=0, row=3, sticky='NSEW')
        game_lbl.grid(column=2, row=3, columnspan=2, sticky='NSEW')
        score_lbl.grid(column=6, row=3, sticky='NSEW')
        status_lbl.grid(column=5, row=3, sticky='NSEW')

        # constant mlb network stream link
        mlb_network = tk.Label(self, text="MLB Network", font=AVG_FONT, fg="red", cursor="hand2")
        mlb_network.grid(column=4, row=0, sticky='NSEW')
        mlb_network.bind("<Button-1>",
                         lambda event:webbrowser.open_new("http://.net/game-1-.html"))

        configure(self, daily_games(self), 7)


app = BaseballStreamsApp()
app.mainloop()

Currently when I ran the function (daily_games) which is the one I want to be automatically updating is initially executed in the (StartPage) class and in the _init_ function at this line

configure(self, daily_games(self), 7)

I would like to have a way to automatically run the (daily_games) function every minute or whenever I specify.

Concerns:

lets say I am able to automatically run the function my question is, would the program keep writing tkinter labels over the previous labels causing my program to put unnecessary load on the computer?

if so how should I go about deleting the previous labels.

Thanks in advance! once again sorry for the long post

I got it working here is what it looks like now

class BaseballStreamsApp(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, "C:\\Users\\Davis Hurd\\PycharmProjects\\baseball_streams\\bbstreams\\mlb_logos\\MLB.ico")
        tk.Tk.wm_title(self, "MLB Streams")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand= True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        frame = StartPage(container, self)

        self.frames[StartPage] = frame

        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        title_lbl = tk.Label(self, text="MLB Streams " + str(date.today()), font=LARGE_FONT)
        time_lbl = tk.Label(self, text="Time", font=AVG_FONT)
        game_lbl = tk.Label(self, text="Game", font=AVG_FONT)
        score_lbl = tk.Label(self, text="Score", font=AVG_FONT)
        status_lbl = tk.Label(self, text="Status", font=AVG_FONT)

        title_lbl.grid(column=2, row=0, columnspan=2, pady=10, padx=10, sticky='NSEW')
        time_lbl.grid(column=0, row=3, sticky='NSEW')
        game_lbl.grid(column=2, row=3, columnspan=2, sticky='NSEW')
        score_lbl.grid(column=6, row=3, sticky='NSEW')
        status_lbl.grid(column=5, row=3, sticky='NSEW')

        # constant mlb network stream link
        mlb_network = tk.Label(self, text="MLB Network", font=AVG_FONT, fg="red", cursor="hand2")
        mlb_network.grid(column=4, row=0, sticky='NSEW')
        mlb_network.bind("<Button-1>",
                         lambda event:webbrowser.open_new("http://bilasport.net/game/mlb-network-vs-formula-1-3366.html"))

        # populates StartPage with Game times, links, and logos
        streams = stream_links.today_streams()
        rows = 5
        for game in streams:
            team_1 = game[1]
            team_2 = game[2]

            logos = team_logos(team_1, team_2)
            logo = logos[0]
            logo2 = logos[1]

            # assigns team one logo
            t1_logo = tk.Label(self, image=logo)
            t1_logo.image = logo
            t1_logo.grid(column=1, row=rows, sticky='NSEW')

            # assigns team two logo
            t2_logo = tk.Label(self, image=logo2)
            t2_logo.image = logo2
            t2_logo.grid(column=4, row=rows, sticky='NSEW')

            # link to stream
            lbl = tk.Label(self, text=team_1 + " vs " + team_2, fg="blue", cursor="hand2", font=AVG_FONT)
            lbl.grid(column=3, row=rows, sticky='NSEW')
            assign_link(lbl, game[0])

            # game start time
            game_start = matchup_info(team_1)[0]
            game_start_lbl = tk.Label(self, text=game_start, font=AVG_FONT, padx=10, pady=10)
            game_start_lbl.grid(column=0, row=rows, sticky='NSEW')

            rows += 2

        # handles the configuration of columns and rows
        configure(self, rows, 7)

        self.daily_games()

    # populates StartPage with updating values such as game Status and Scores
    def daily_games(self):
        streams = stream_links.today_streams()
        row = 5
        for game in streams:
            team_1 = game[1]

            # uses mlbgame package to get status, and scores
            game_info = matchup_info(team_1)
            game_status_lbl = tk.Label(self, text=game_info[1], font=AVG_FONT)
            game_status_lbl.grid(column=5, row=row, sticky='NSEW')
            score_lbl = tk.Label(self, text=game_info[2] + " - " + game_info[3], font=AVG_FONT, padx=10, pady=10)
            score_lbl.grid(column=6, row=row, sticky='NSEW')

            row += 2

        self.after(120000, self.daily_games)


app = BaseballStreamsApp()
app.mainloop()

this seems to be working.. not sure how efficient it is though

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM