|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Aden 于 2022-7-26 00:18 编辑
求各位大神指教,下面代码中我将代码封装后,在代码备注的地方写了个函数,调用Entry的get(),无法调用,但是封装前却可以调用,同样的语法封装class后无法调用是为啥啊,咋解决呢
import tkinter as tk
import time
from tkinter import *
class Root_page:
def off_work(self):
self.page_main.grid_forget()
self.page_wktime = tk.Frame(self.window)
self.page_wktime.grid(row = 0,column=0,padx =78,pady =60)
now_time= time.strftime('%Y-%m-%d %H:%M:%S')
off_work_time = now_time[0:11] + '17:30:00'
make_off_time = time.mktime(time.strptime(off_work_time,'%Y-%m-%d %H:%M:%S'))
interval_time = make_off_time - time.time()
off_min = divmod(interval_time,60)
time_label_1 = tk.Label(self.page_wktime,text=f"现在是 {(time.strftime('%H点%M分%S秒'))}",width= 25,height=2)
time_label_2 = tk.Label(self.page_wktime,text=f"距离下班还有 {int(off_min[0])}分钟{round(off_min[1],3)}秒",width= 25,height=2)
time_label_1.grid(row = 0,column=1,padx =30 ,pady =10)
time_label_2.grid(row = 1,column=1,padx =30 ,pady =10)
tk.Button(self.page_wktime,text='返回',width=15,command = self.back_main_1).grid(row = 2,column=1,padx =30 ,pady =30)
def back_main_1(self):
self.page_wktime.grid_forget()
self.page_main.grid(row = 0,column=1,padx =130,pady =30)
def back_main_2(self):
self.page_set_time.place_forget()
self.page_main.grid(row = 0,column=1,padx =130,pady =30)
def get_off_time(self):
print(self.enter_int_1.get())
print(self.enter_int_2.get())
def set_offtime(self):
self.page_main.grid_forget()
self.page_set_time = tk.Frame(self.window)
self.page_set_time.place(relx = 0.5,rely=0.3,anchor=CENTER,relheight=0.75,relwidth=0.75)
tk.Label(self.page_set_time,text = '时').place(x = 130,y=125)
tk.Label(self.page_set_time,text = '分').place(x = 200,y=125)
self.t1 = tk.StringVar()
self.t2 = tk.StringVar()
self.enter_int_1 = tk.Entry(self.page_set_time,textvariable=self.t1,width = 4).place(x=95,y=125)
self.enter_int_2 = tk.Entry(self.page_set_time,textvariable=self.t2,width = 4).place(x =165,y=125)
tk.Label(self.page_set_time,text = '注意:请使用24小时制,不足2位数请加0补位').place(relx = 0.5,rely=0.65,anchor=CENTER)
tk.Button(self.page_set_time,text='返回',width=8,command = self.back_main_2).place(relx = 0.7,rely=0.8,anchor=CENTER)
tk.Button(self.page_set_time,text='确定',width=8,command = self.get_off_time).place(relx = 0.3,rely=0.8,anchor=CENTER)
#上一行command 调用函数get_off_time时无法调用tkinter库get()属性
def __init__(self,master):
self.window = master
self.window.title('摸鱼 Demo 1.0')
self.window.geometry('400x400')
self.window.resizable(width=False, height=False)
self.page_main = tk.Frame(self.window)
self.page_main.grid(row = 0,column=1,padx =130,pady =30)
tk.Button(self.page_main,text='还有多久下班',width=15,command = self.off_work).grid(row = 0,column=1,padx =10 ,pady =10)
tk.Button(self.page_main,text='设置下班时间',width=15,command = self.set_offtime).grid(row = 1,column=1,padx =10 ,pady =10)
tk.Button(self.page_main,text='中午吃什么',width=15,).grid(row = 2,column=1,padx =10 ,pady =10)
tk.Button(self.page_main,text='退出',width=15,command = self.window.quit).grid(row = 5,column=1,padx =10 ,pady =10)
if __name__ == '__main__':
window = tk.Tk()
Root_page(master = window)
window.mainloop()
果然经典错误,我之前也犯过 self.enter_int_1 = tk.Entry(self.page_set_time,textvariable=self.t1,width = 4).place(x=95,y=125)
self.enter_int_2 = tk.Entry(self.page_set_time,textvariable=self.t2,width = 4).place(x =165,y=125
这两行self.enter_int_1和self.enter_int_2被赋值成了什么,tk.Entry还是place函数执行后的返回值,答案是place函数执行后的返回值也就是None
而None没有get方法,所以报了错
解决方法:将self.enter_int_1 = tk.Entry(self.page_set_time,textvariable=self.t1,width = 4)与self.enter_int_1.place(x=95,y=125)分开写
代码: import tkinter as tk
import time
from tkinter import *
class Root_page:
def off_work(self):
self.page_main.grid_forget()
self.page_wktime = tk.Frame(self.window)
self.page_wktime.grid(row = 0,column=0,padx =78,pady =60)
now_time= time.strftime('%Y-%m-%d %H:%M:%S')
off_work_time = now_time[0:11] + '17:30:00'
make_off_time = time.mktime(time.strptime(off_work_time,'%Y-%m-%d %H:%M:%S'))
interval_time = make_off_time - time.time()
off_min = divmod(interval_time,60)
time_label_1 = tk.Label(self.page_wktime,text=f"现在是 {(time.strftime('%H点%M分%S秒'))}",width= 25,height=2)
time_label_2 = tk.Label(self.page_wktime,text=f"距离下班还有 {int(off_min[0])}分钟{round(off_min[1],3)}秒",width= 25,height=2)
time_label_1.grid(row = 0,column=1,padx =30 ,pady =10)
time_label_2.grid(row = 1,column=1,padx =30 ,pady =10)
tk.Button(self.page_wktime,text='返回',width=15,command = self.back_main_1).grid(row = 2,column=1,padx =30 ,pady =30)
def back_main_1(self):
self.page_wktime.grid_forget()
self.page_main.grid(row = 0,column=1,padx =130,pady =30)
def back_main_2(self):
self.page_set_time.place_forget()
self.page_main.grid(row = 0,column=1,padx =130,pady =30)
def get_off_time(self):
print(self.enter_int_1.get())
print(self.enter_int_2.get())
def set_offtime(self):
self.page_main.grid_forget()
self.page_set_time = tk.Frame(self.window)
self.page_set_time.place(relx = 0.5,rely=0.3,anchor=CENTER,relheight=0.75,relwidth=0.75)
tk.Label(self.page_set_time,text = '时').place(x = 130,y=125)
tk.Label(self.page_set_time,text = '分').place(x = 200,y=125)
self.t1 = tk.StringVar()
self.t2 = tk.StringVar()
self.enter_int_1 = tk.Entry(self.page_set_time,textvariable=self.t1,width = 4)
self.enter_int_1.place(x=95,y=125)
self.enter_int_2 = tk.Entry(self.page_set_time,textvariable=self.t2,width = 4)
self.enter_int_2.place(x =165,y=125)
tk.Label(self.page_set_time,text = '注意:请使用24小时制,不足2位数请加0补位').place(relx = 0.5,rely=0.65,anchor=CENTER)
tk.Button(self.page_set_time,text='返回',width=8,command = self.back_main_2).place(relx = 0.7,rely=0.8,anchor=CENTER)
tk.Button(self.page_set_time,text='确定',width=8,command = self.get_off_time).place(relx = 0.3,rely=0.8,anchor=CENTER)
#上一行command 调用函数get_off_time时无法调用tkinter库get()属性
def __init__(self,master):
self.window = master
self.window.title('摸鱼 Demo 1.0')
self.window.geometry('400x400')
self.window.resizable(width=False, height=False)
self.page_main = tk.Frame(self.window)
self.page_main.grid(row = 0,column=1,padx =130,pady =30)
tk.Button(self.page_main,text='还有多久下班',width=15,command = self.off_work).grid(row = 0,column=1,padx =10 ,pady =10)
tk.Button(self.page_main,text='设置下班时间',width=15,command = self.set_offtime).grid(row = 1,column=1,padx =10 ,pady =10)
tk.Button(self.page_main,text='中午吃什么',width=15,).grid(row = 2,column=1,padx =10 ,pady =10)
tk.Button(self.page_main,text='退出',width=15,command = self.window.quit).grid(row = 5,column=1,padx =10 ,pady =10)
if __name__ == '__main__':
window = tk.Tk()
Root_page(master = window)
window.mainloop()
|
|