问题描述
- python tkinter 组件config报错
-
import sys import time from tkinter import * clock_lab = None def refresh_time(): clock_lab.config(text = time.localtime(time.time())) myclock = Tk() myclock.title("myclock") myclock.geometry("400x200") clock_lab = Label(myclock,text= time.localtime(time.time())).pack() clock_bt = Button(myclock, text="clock", command =lambda:refresh_time()).pack() myclock.mainloop()
按过按钮之后报错,有没有人知道是怎么回事
Exception in Tkinter callback
Traceback (most recent call last):
File "C:Python34lib kinter__init__.py", line 1533, in call
return self.func(*args)
File "F:/code/python/clock.py", line 14, in
clock_bt = Button(myclock, text="clock", command =lambda:refresh_time()).pack()
File "F:/code/python/clock.py", line 8, in refresh_time
clock_lab.config(text = time.localtime(time.time()))
AttributeError: 'NoneType' object has no attribute 'config'
解决方案
import sys
import time
from tkinter import *
clock_lab = None
def refresh_time():
clock_lab.config(text = time.localtime(time.time()))
myclock = Tk()
myclock.title("myclock")
myclock.geometry("400x200")
clock_lab = Label(myclock,text= time.localtime(time.time()))
clock_lab.pack()
clock_bt = Button(myclock, text="clock", command =lambda:refresh_time())
clock_bt.pack()
myclock.mainloop()
把pack()单独调用就好了,有没有大神解释一下是怎么回事啊
解决方案二:
前者是pack返回对象,而你后面的做法是先得到Label对象。所有应该是第二种方法对。
解决方案三:
Python Tkinter PanedWindow 控件
时间: 2024-11-08 18:16:05