gpt4 book ai didi

python - 需要帮助每分钟更新一个 tkinter 函数

转载 作者:太空宇宙 更新时间:2023-11-03 20:55:56 24 4
gpt4 key购买 nike

问题:

我正在尝试使用 tkinter 创建一个程序来显示每日棒球比赛、开始时间和实时比分。问题在于实时分数,因为我无法找到如何集成更新保存当前分数的函数(daily_games)的方法。

我只编程了几个月,所以我的代码可能到处都是......对于这么长的帖子也感到抱歉。

到目前为止我已经尝试过:我尝试过使用 after() 和 tkinters update() 但我不确定如何集成它。

代码:

# 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()
<小时/>

目前,当我运行函数 (daily_games) 时,我想要自动更新的函数最初在 (StartPage) 类和这一行的 _init_ 函数中执行

configure(self, daily_games(self), 7)

我希望有一种方法可以每分钟或在我指定的时间自动运行 (daily_games) 函数。

担忧:

假设我能够自动运行该函数,我的问题是,程序是否会继续在以前的标签上写入 tkinter 标签,导致我的程序给计算机带来不必要的负载?

如果是这样,我应该如何删除以前的标签。

提前致谢!再次对这么长的帖子表示歉意

最佳答案

我在这里工作就是现在的样子

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

这似乎有效......但不确定它的效率如何

关于python - 需要帮助每分钟更新一个 tkinter 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56029823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com