Tkinter - Spinbox - 如何将跟踪值放入变量中?
Tkinter - Spinbox - How can I get traced values into a variable?
如何将下面修改后的代码 () 中的 Tkinter Spinbox 的跟踪值放入变量中,以便我可以将其用作时间戳?
我添加了印刷品以视觉确认实际正在跟踪更改
但是当我添加
b = tk.Button(text='get', command=App(root).trace_varhour)
b.pack()
就在 root.mainloop()
之前它返回了 13,这是默认值而不是跟踪值。
import tkinter as tk
class App(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.hourstr = tk.StringVar(self, '13')
self.hour = tk.Spinbox(self, from_=13, to=23, wrap=True, textvariable=self.hourstr, width=2, state='readonly')
self.minstr = tk.StringVar(self, '30')
self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2)
self.secstr = tk.StringVar(self, '00')
self.sec = tk.Spinbox(self, from_=0, to=59, wrap=True,textvariable=self.secstr, width=2)
self.last_valueSec = ''
self.last_value = ''
self.last_valuehour = ''
self.hourstr.trace('w', self.trace_varhour)
self.minstr.trace('w', self.trace_var)
self.secstr.trace('w', self.trace_varsec)
self.hour.grid()
self.min.grid(row=0, column=1)
self.sec.grid(row=0, column=2)
def trace_varhour(self, *args):
self.last_valuehour = self.hourstr.get()
print(self.last_valuehour)
return self.last_valuehour
def trace_var(self, *args):
self.last_value = self.minstr.get()
# return self.last_value
def trace_varsec(self, *args):
self.last_valueSec = self.secstr.get()
# return self.last_valueSec
root = tk.Tk()
App(root).pack()
b = tk.Button(text='get', command=App(root).trace_varhour)
b.pack()
root.mainloop()
经过反复试验,我找到了解决我问题的方法,希望对任何人有帮助:
import tkinter as tk
class App(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.hourstr = tk.StringVar(self, '13')
self.hour = tk.Spinbox(self, from_=13, to=23, wrap=True, textvariable=self.hourstr, width=2, state='readonly')
self.minstr = tk.StringVar(self, '30')
self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2)
self.secstr = tk.StringVar(self, '00')
self.sec = tk.Spinbox(self, from_=0, to=59, wrap=True,textvariable=self.secstr, width=2)
self.last_valueSec = ''
self.last_value = ''
self.last_valuehour = ''
self.hourstr.trace('w', self.trace_varhour)
self.minstr.trace('w', self.trace_var)
self.secstr.trace('w', self.trace_varsec)
self.hour.grid()
self.min.grid(row=0, column=1)
self.sec.grid(row=0, column=2)
def trace_varhour(self, *args):
self.last_valuehour = self.hourstr.get()
def trace_var(self, *args):
self.last_value = self.minstr.get()
def trace_varsec(self, *args):
self.last_valueSec = self.secstr.get()
root = tk.Tk()
app = App(root)
app.pack()
def get_time():
get_time = app.last_valuehour + ":" + app.last_value + ":" + app.last_valueSec
print(get_time)
b = tk.Button(text='get', command=get_time)
b.pack()
root.mainloop()
如何将下面修改后的代码 (
我添加了印刷品以视觉确认实际正在跟踪更改 但是当我添加
b = tk.Button(text='get', command=App(root).trace_varhour)
b.pack()
就在 root.mainloop()
之前它返回了 13,这是默认值而不是跟踪值。
import tkinter as tk
class App(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.hourstr = tk.StringVar(self, '13')
self.hour = tk.Spinbox(self, from_=13, to=23, wrap=True, textvariable=self.hourstr, width=2, state='readonly')
self.minstr = tk.StringVar(self, '30')
self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2)
self.secstr = tk.StringVar(self, '00')
self.sec = tk.Spinbox(self, from_=0, to=59, wrap=True,textvariable=self.secstr, width=2)
self.last_valueSec = ''
self.last_value = ''
self.last_valuehour = ''
self.hourstr.trace('w', self.trace_varhour)
self.minstr.trace('w', self.trace_var)
self.secstr.trace('w', self.trace_varsec)
self.hour.grid()
self.min.grid(row=0, column=1)
self.sec.grid(row=0, column=2)
def trace_varhour(self, *args):
self.last_valuehour = self.hourstr.get()
print(self.last_valuehour)
return self.last_valuehour
def trace_var(self, *args):
self.last_value = self.minstr.get()
# return self.last_value
def trace_varsec(self, *args):
self.last_valueSec = self.secstr.get()
# return self.last_valueSec
root = tk.Tk()
App(root).pack()
b = tk.Button(text='get', command=App(root).trace_varhour)
b.pack()
root.mainloop()
经过反复试验,我找到了解决我问题的方法,希望对任何人有帮助:
import tkinter as tk
class App(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.hourstr = tk.StringVar(self, '13')
self.hour = tk.Spinbox(self, from_=13, to=23, wrap=True, textvariable=self.hourstr, width=2, state='readonly')
self.minstr = tk.StringVar(self, '30')
self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2)
self.secstr = tk.StringVar(self, '00')
self.sec = tk.Spinbox(self, from_=0, to=59, wrap=True,textvariable=self.secstr, width=2)
self.last_valueSec = ''
self.last_value = ''
self.last_valuehour = ''
self.hourstr.trace('w', self.trace_varhour)
self.minstr.trace('w', self.trace_var)
self.secstr.trace('w', self.trace_varsec)
self.hour.grid()
self.min.grid(row=0, column=1)
self.sec.grid(row=0, column=2)
def trace_varhour(self, *args):
self.last_valuehour = self.hourstr.get()
def trace_var(self, *args):
self.last_value = self.minstr.get()
def trace_varsec(self, *args):
self.last_valueSec = self.secstr.get()
root = tk.Tk()
app = App(root)
app.pack()
def get_time():
get_time = app.last_valuehour + ":" + app.last_value + ":" + app.last_valueSec
print(get_time)
b = tk.Button(text='get', command=get_time)
b.pack()
root.mainloop()