PythonTkinter正在尝试删除标签

2024-10-01 04:44:02 发布

您现在位置:Python中文网/ 问答频道 /正文

import time 
import tkinter
from tkinter import *
import tkinter as tk
 
stonestotal = 0
def metricimperial(Amount):
    Amount = stonestotal.get()
    global gramstotal
    gramstotal = Amount * 0.00220462
    print(gramstotal)
    final()
    
def choice():
    conversion = input("metric or imperial conversion ")
    
def imperialmetric(Amount):
    Amount = stonestotal.get()
    global gramstotal
    gramstotal = Amount * 453.592
    print(gramstotal)
    final()
 
window = Tk()
 
Labelframe = LabelFrame(window, text="this is a LabelFrame").grid()
stonestotal = tkinter.IntVar(value=1)
inputbox = Spinbox(Labelframe, textvariable=stonestotal, from_=0, to = 99999999999999999).grid(row = 0, column = 0)
Amount = stonestotal.get()
imperial = Button(Labelframe,text="imperial to metric",command = lambda:imperialmetric(Amount)).grid(row=1,column=0)
metric = Button(Labelframe,text="metric to imperial",command = lambda:metricimperial(Amount)).grid(row=1,column=1)
def final():
    label = Label(Labelframe, text = gramstotal).grid()
    
    label.after(5, label.master.destroy)
    
 
 
window.mainloop()

当我运行代码时,它在输出转换后的值的大部分时间内工作,但是它不会删除当前值,而是将下一个输出无限期地放在堆栈下方,同时给出错误,我得到一个错误,说明:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:/Python39/yr11 code.py", line 31, in <lambda>
    metric = Button(Labelframe,text="metric to imperial",command = lambda:metricimperial(Amount)).grid(row=1,column=1)
  File "C:/Python39/yr11 code.py", line 12, in metricimperial
    final()
  File "C:/Python39/yr11 code.py", line 35, in final
    label.after(5, label.master.destroy)
AttributeError: 'NoneType' object has no attribute 'after'

我不知道是什么原因造成的任何帮助都将不胜感激


Tags: textinimporttkinterdefmetricamountlabel
1条回答
网友
1楼 · 发布于 2024-10-01 04:44:02

所有几何图形管理器:grid(),pack(),place()返回None,或不返回任何内容。最后,变量label被赋值None

.grid()移到下一行

label = Label(Labelframe, text = gramstotal)
label.grid()

相关问题 更多 >