AttributeError: '_tkinter.tkapp' 对象没有属性 'root' [英] AttributeError: '_tkinter.tkapp' object has no attribute 'root'

查看:159
本文介绍了AttributeError: '_tkinter.tkapp' 对象没有属性 'root'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Trgbhtml ( TApplication ):
def __init__(self):
    TApplication.__init__(self, "RGB")
    self.size = (300, 120)
    self.position = (200, 200)
    f = ("GOST type A", 14)
    self.lblR = TLabel(self, text="R = ", font=f)
    self.lblR.position= (5, 5)
    self.lblG = TLabel(self, text="G = ", font=f)
    self.lblG.position = (5, 30)
    self.lblB = TLabel(self, text="B = ", font=f)
    self.lblB.position = (5, 55)
    fc = ("GOST type A", 16, "bold")
    self.rgbLabel = TLabel(self, text="#000000", font=fc, fg="navy")
    self.rgbLabel.position = (100, 5)
    self.rgbRect = TLabel(self, text="", width=150, height=60)
    self.rgbRect.position = (105, 35)
    self.rEdit = TEdit(self, font=f, width=50)
    self.rEdit.position = (45, 5)
    self.rEdit.text = "123"
    self.gEdit = TEdit(self, font=f, width=50)
    self.gEdit.position = (45, 30)
    self.gEdit.text = "56"
    self.bEdit = TEdit(self, font=f, width=50)
    self.bEdit.text = "80"
    self.bEdit.position = (45, 55)
    self.rEdit.onChange = self.onChange
    self.gEdit.onChange = self.onChange
    self.bEdit.onChange = self.onChange
def onChange(self, sender):
    r=int(self.rEdit.text)
    g=int(self.gEdit.text)
    b=int(self.bEdit.text)
    s="#{:02x}{:02x}{:02x}".format(r, g, b)
    self.rgbRect.background=s
    self.rgbLabel.text=s
app=Trgbhtml()
app.Run()

表示尝试启动失败怎么解决?

Indicates fails when trying to start how to fix it?

error: AttributeError: '_tkinter.tkapp' object has no attribute 'root' ;app=Trgbhtml() ;self.lblR = TLabel(self, text="R = ", font=f)

error: AttributeError: '_tkinter.tkapp' object has no attribute 'root' ; app=Trgbhtml() ; self.lblR = TLabel(self, text="R = ", font=f)

推荐答案

错误提示您正在使用 self 作为 self.lblR 的父级,但 self 不是小部件.您只能将小部件用作其他小部件的父级.

The error is telling you that you're using self as the parent of self.lblR, but self isn't a widget. You can only use widgets as parents of other widgets.

这篇关于AttributeError: '_tkinter.tkapp' 对象没有属性 'root'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆