【求助】封装后无法使用tkinter库中的get()属性
本帖最后由 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 + '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)}分钟{round(off_min,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()
缩进问题
import tkinter as tk
import time
from tkinter import *
class Root_page:
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,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() 估计不小心点了 tabel 键缩进问题 qq1151985918 发表于 2022-7-25 22:18
估计不小心点了 tabel 键缩进问题
缩进应该没毛病,在vscode里标的很齐,层次我也检查过了 临时号 发表于 2022-7-25 22:17
缩进问题
我复制的时候第6行缩进错了,源码实际是没错的,但是问题不是缩进的,是找不到库的属性。。。 Aden 发表于 2022-7-26 00:03
我复制的时候第6行缩进错了,源码实际是没错的,但是问题不是缩进的,是找不到库的属性。。。
我没报这个错误啊,只报了找不到off_work函数
D:\command\Python>python test.py
Traceback (most recent call last):
File "test.py", line 270, in <module>
Root_page(master = window)
File "test.py", line 258, in __init__
tk.Button(self.page_main,text='还有多久下班',width=15,command = self.off_wor
k).grid(row = 0,column=1,padx =10 ,pady =10)
AttributeError: 'Root_page' object has no attribute 'off_work' 临时号 发表于 2022-7-26 00:06
我没报这个错误啊,只报了找不到off_work函数
因为我复制的时候缩进错了,所以找不到,调整了缩进,就找到了,我把修改过的更新了,你再看看,我这边错误类型为下面的样子,就是找不到get()
File "d:\Python 文件\【GUI】tkinter 实例\【UI学习】摸鱼Demo.py", line 38, in get_off_time
print(self.enter_int_1.get())
AttributeError: 'NoneType' object has no attribute 'get' Aden 发表于 2022-7-26 00:20
因为我复制的时候缩进错了,所以找不到,调整了缩进,就找到了,我把修改过的更新了,你再看看,我这边错 ...
果然经典错误,我之前也犯过
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 + '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)}分钟{round(off_min,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()
临时号 发表于 2022-7-26 00:35
果然经典错误,我之前也犯过
这两行self.enter_int_1和self.enter_int_2被赋值成了什么,tk.Entry还是 ...
是的是的,这回可以了,大佬牛啊,我明白了,谢谢大佬
页:
[1]