我在Python中的基本概念和措辞方面取得了一些进展,但是当我们开始将Tkinter添加到混合中时,我有点迷失。 BryanOakley为一个关于多个窗口和Tkinter的问题做出了巨大贡献。我举了他的例子,把它和我见过的其他一些例子结合起来,并开始制作一个带闹钟的闹钟,当闹铃时间响起时,它会改变时钟背景。问题是,我只是看不到树木通过森林,看不到如何:
A)从Tkinter窗口获取输入请求 B)将警报类中的用户输入调用到时钟类中以触发窗口背景更改。
我认为我的问题是我不完全了解嵌套不同方法的位置以及如何获取存储用户数据的变量,并从字符串到时间格式化。我想我只需要一些指导,但如果你认为我已经超过了我的头,那么请指出我的浅层,这样我就可以继续练习了。这是我的想法,把这个或类似的东西放到tick()方法中,当然我不知道输入甚至会输入到Tkinter中。我正在疯狂。帮助:
def wake(self):
self.__init__(self, hour, min)
self.hour = hour
self.min = min
input("what is the hour?",
input("What is the min?",
hour = datetime.datetime.now().strftime("%I")
min = datetime.datetime.now().strftime("%M")
if self.hour and self.min == time1():
label.config(text = time1, bg = "green")
else:
label.config(text = time1, bg = "red")
这是我到目前为止所拥有的:
import tkinter as tk
from tkinter import font as tkfont
from tkinter import messagebox
import time
import datetime
class digitalClockApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=100, weight="bold", slant="italic")
container = tk.Frame(self)
container.pack(side = "top", fill= "both", expand =1)
container.grid_rowconfigure(0,weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (introPage, clock, alarm):
page_name = F.__name__
frame = F(parent = container, controller = self)
self.frames[page_name] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.geometry("640x480")
self.show_frame("introPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class introPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text = "Welcome", bg = "black", fg = "white", font = controller.title_font)
label.pack(side = "top", fill = "both", expand = 1)
button1 = tk.Button(self, text = "Go to Clock", command = lambda: controller.show_frame("clock"))
button2 = tk.Button(self, text = "Go to Set Alarm", command = lambda: controller.show_frame("alarm"))
button1.pack(side = "left")
button2.pack(side = "left")
class clock(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text = "", bg = "black", fg = "white", font = controller.title_font)
label.pack(side = "top", fill = "both", expand = 1)
button = tk.Button(self, text = "Home Screen", command = lambda: controller.show_frame("introPage"))
button.pack()
def tick(time1 = ""):
time2 = time.strftime("%I:%M:%S")
if time2 != time1:
time1 = time2
label.config(text = time1, bg = "red")
label.after(200, tick)
tick()
class alarm(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text = "", bg = "black", fg = "white", font = controller.title_font)
label.pack(side = "top", fill = "both", expand = 1)
def wake():
button = tk.Button(self, text = "Home Screen", command = lambda: controller.show_frame("introPage"))
button.pack()
if __name__ == "__main__":
app = digitalClockApp()
app.mainloop()
答案 0 :(得分:0)
坦率地说,我认为切换帧的概念对于学习使用tkinter的人来说是一个糟糕的选择。这个例子从来没有打算用作初学者的起点,特别是对于像闹钟这样简单的东西。它增加了很多开销,但收效甚微。
我的建议是从一个窗口重新开始。添加小时和分钟的条目小部件,显示时钟的标签和启动时钟的按钮。这就是你完成这项练习所需要的一切。它可能是几十行代码。
您的tick
方法可能会或多或少地使用,但如果您只显示整秒的时间,我没有看到每秒更新时钟五次。
只有在您开始工作之后,才能开始尝试使用高级架构,例如“切换帧”代码。