AttributeError:Label实例没有属性' insert'

时间:2017-02-26 23:13:42

标签: python tkinter

我的代码的目标是让滑块在更改其编号时将文本插入文本框。但是,我似乎无法找到导致"属性错误的原因:标签实例没有属性'插入'""错误。

# -*- coding: utf-8 -*-
import Tkinter 
from Tkinter import *
root = Tkinter.Tk() #The root/base

def math(): #This is the command that does the math
    text.insert(Tkinter.END, base +height + radius)
    text.see(Tkinter.END)

text = Text(root, width=10, height=20, background='#FFFFFF')
text.grid(row=0, rowspan=2, column=2)

text = Tkinter.Label(root, text='Change slider\nTo Desired\nValue')
text.grid(row=0, column=1)
####
#Variables
####
base = Tkinter.IntVar()
base.set(0)
height = Tkinter.IntVar()
height.set(0)
radius = Tkinter.IntVar()
radius.set(0)
####
#Sliders
####

base_slider = Tkinter.Scale(root, from_=0, to=500, variable=base, 
                       orient=Tkinter.HORIZONTAL,   
                       label='Base', command=math)
base_slider.grid(row=1, column=1)

height_slider = Tkinter.Scale(root, from_=0, to=500, variable=height, 
                       orient=Tkinter.HORIZONTAL,   
                       label='Height', command=math)
height_slider.grid(row=2, column=1)

radius_slider = Tkinter.Scale(root, from_=0, to=500, variable=radius, 
                       orient=Tkinter.HORIZONTAL,   
                       label='Radius', command=math)
radius_slider.grid(row=3, column=1)

root.mainloop()

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您有两个名称相同的变量。

您的LabelText小部件都称为text。因为标签是第二个要分配的对象,它是从那时开始与变量text相关联的对象。

您认为自己插入了Text窗口小部件,但它是Label,缺少该方法。

要修复它,请为小部件指定不同的名称。

修正了代码,对问题的评论提供了更多信息。

# -*- coding: utf-8 -*-
import Tkinter 
from Tkinter import *
root = Tkinter.Tk() #The root/base

text = Text(root, width=10, height=20, background='#FFFFFF')
text.grid(row=0, rowspan=2, column=2)

label = Tkinter.Label(root, text='Change slider\nTo Desired\nValue')
label.grid(row=0, column=1)

def math(arg): #This is the command that does the math
    text.insert(Tkinter.END, base.get() +height.get() + radius.get())
    text.see(Tkinter.END)

####
#Variables
####
base = Tkinter.IntVar()
base.set(0)
height = Tkinter.IntVar()
height.set(0)
radius = Tkinter.IntVar()
radius.set(0)
####
#Sliders
####

base_slider = Tkinter.Scale(root, from_=0, to=500, variable=base,
                       orient=Tkinter.HORIZONTAL,
                       label='Base', command=math)
base_slider.grid(row=1, column=1)

height_slider = Tkinter.Scale(root, from_=0, to=500, variable=height,
                       orient=Tkinter.HORIZONTAL,   
                       label='Height', command=math)
height_slider.grid(row=2, column=1)

radius_slider = Tkinter.Scale(root, from_=0, to=500, variable=radius,
                       orient=Tkinter.HORIZONTAL,
                       label='Radius', command=math)
radius_slider.grid(row=3, column=1)

root.mainloop()