gpt4 book ai didi

python - AttributeError: 'Application' 对象没有属性 'label'

转载 作者:行者123 更新时间:2023-12-01 08:41:27 24 4
gpt4 key购买 nike

我的代码出现此错误,有人可以帮助我。当我将标签放置到 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()

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

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:“应用程序”对象没有属性“标签”

最佳答案

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

关于python - AttributeError: 'Application' 对象没有属性 'label',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53471388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com