问题:
我正在尝试使用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标签,从而导致我的程序对计算机造成不必要的负担?
如果是这样,我应该如何删除以前的标签。
提前谢谢!再次对冗长的帖子表示歉意
答案 0 :(得分:0)
我知道它在这里工作了,现在看起来像
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()
这似乎在起作用。.不确定其效率如何