【问题标题】:AttributeError: 'Application' object has no attribute 'label'AttributeError:“应用程序”对象没有属性“标签”
【发布时间】:2019-04-27 12:34:47
【问题描述】:

我的代码给出了这个错误,有人可以帮助我。当我将标签放置到 rand_num() 或 tool() 时,它给出相同的错误并不重要。 如何赋予标签属性?

from tkinter import *
import random

class Application(object):
def __init__(self):
    self.rand_num()
    self.tool()

def tool(self):
    self.label = Label(fg="white", bg="#61380B", font="Helvetica 12 bold")
    self.label["text"] = self.list

    self.label.pack()
    self.open = Button(text='Hit', command=self.rand_num())
    self.open.pack()
    self.ok = Button(text='Exit', command=root.quit)
    self.ok.pack()

def rand_num(self):
    self.list = []
    for i in range(6):
        rand = random.randint(1, 49)
        if rand not in self.list:
            self.list.append(rand)
    self.list.sort()
    self.label["text"] = self.list


if __name__ == '__main__':
    root = Tk()
    root.geometry('300x100+500+100')
    app = Application()
    mainloop()

edit:我是这样编辑的,效果很好!

from tkinter import *
import random


class Application(object):

def __init__(self):
    self.tool() #Deleted that line

def rand_num(self):
    self.list = []
    for i in range(6):
        rand = random.randint(1, 49)
        if rand not in self.list:
            self.list.append(rand)
    self.list.sort()
    self.label["text"] = self.list

def tool(self):
    self.label = Label(text='Please enter hit', #Added 'text' attribute
                       fg="white",
                       bg="#61380B",
                       font="Helvetica 12 bold")
    #Deleted that line

    self.label.pack()
    self.open = Button(text='Hit', command=self.rand_num)
    self.open.pack()
    #self.ok = Button(text='Exit', command=root.quit)
    #self.ok.pack()




if __name__ == '__main__':
    root = Tk()
    root.geometry('300x100+500+100')
    app = Application()
    mainloop()

更改的部分在上面 AttributeError:“应用程序”对象没有属性“标签”

【问题讨论】:

    标签: python tkinter attributeerror


    【解决方案1】:

    您在tool() 方法中定义self.label。该错误可能是因为您在tool() 方法之前调用了rand_num() 方法。

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 2019-01-09
      • 2019-01-05
      • 2011-08-11
      相关资源
      最近更新 更多