#python #tkinter
Вопрос:
Я пишу простую программу-карточку ООП для задания. Как это должно работать: Пользователь нажимает кнопку с надписью «Процессор» (cputbut в коде), и метка в поле ответа изменится на определение процессора. В этом случае я хотел бы, чтобы в целях тестирования было указано «Ответ изменился», однако, когда я нажимаю кнопку, она выдает мне эту ошибку:
line 84, in cpubut_command
self.answerbox["text"]= "hello"
AttributeError: 'App' object has no attribute 'answerbox'
Я разместил комментарии там, где указаны свойства кнопки, поля для ответов и функции, предназначенной для изменения текста.
Я не очень хорошо знаком с tkinter и не силен в ООП, поэтому буду признателен за любую помощь.
class App:
def __init__(self, root):
#setting title
root.title("Flash Cards - Computing hardware")
#setting window size
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d %d %d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
cpubut=tk.Button(root)
cpubut["bg"] = "#cc99c9"
ft = tkFont.Font(family='Times',size=10)
cpubut["font"] = ft
cpubut["fg"] = "#000000"
cpubut["justify"] = "center" #On this button press, the label
cpubut["text"] = "The CPU" #should change the label
cpubut.place(x=30,y=50,width=141,height=33)
cpubut["command"] = self.cpubut_command
memorybut=tk.Button(root)
memorybut["bg"] = "#9ec1cf"
ft = tkFont.Font(family='Times',size=10)
memorybut["font"] = ft
memorybut["fg"] = "#000000"
memorybut["justify"] = "center"
memorybut["text"] = "Memory"
memorybut.place(x=30,y=100,width=141,height=31)
memorybut["command"] = self.memorybut_command
iodevicesbut=tk.Button(root)
iodevicesbut["bg"] = "#9ee09e"
ft = tkFont.Font(family='Times',size=10)
iodevicesbut["font"] = ft
iodevicesbut["fg"] = "#000000"
iodevicesbut["justify"] = "center"
iodevicesbut["text"] = "I/O Devices"
iodevicesbut.place(x=30,y=150,width=141,height=30)
iodevicesbut["command"] = self.iodevicesbut_command
motherboardbut=tk.Button(root)
motherboardbut["bg"] = "#fdfd97"
ft = tkFont.Font(family='Times',size=10)
motherboardbut["font"] = ft
motherboardbut["fg"] = "#000000"
motherboardbut["justify"] = "center"
motherboardbut["text"] = "Motherboard"
motherboardbut.place(x=30,y=200,width=142,height=30)
motherboardbut["command"] = self.motherboardbut_command
storagebut=tk.Button(root)
storagebut["bg"] = "#feb144"
ft = tkFont.Font(family='Times',size=10)
storagebut["font"] = ft
storagebut["fg"] = "#000000"
storagebut["justify"] = "center"
storagebut["text"] = "Storage device"
storagebut.place(x=30,y=250,width=141,height=30)
storagebut["command"] = self.storagebut_command
answerbox=tk.Label(root)
ft = tkFont.Font(family='Times',size=10)
answerbox["font"] = ft
answerbox["fg"] = "#333333" ## properties of the answerbox label
answerbox["justify"] = "center"
answerbox["text"] = "label"
answerbox.place(x=220,y=10,width=240,height=325)
helplab=tk.Label(root)
ft = tkFont.Font(family='Times',size=10)
helplab["font"] = ft
helplab["fg"] = "#333333"
helplab["justify"] = "center"
helplab["text"] = "Press each button to see thendefinitions and the functionnof each device"
helplab.place(x=20,y=290,width=159,height=76)
def cpubut_command(self): ## Function in question
self.answerbox["text"]= "The answer has changed"
Комментарии:
1. это говорит вам о том , что нет такой вещи, как
self.answerbox
вам нужно использоватьself.
префикс (стандартный, и вы должны следовать ему) при объявлении переменной, если вы хотите, чтобы она была атрибутом экземпляра, тогда вы можете получить к ней доступ в любом месте класса