所以我是tKinter的新手以及在python中使用我所使用的程度。我已就这个主题做了很多研究,但我很难将它们放在一起。问题是;我在我的主类下有def函数,第一个是def class_Window(创建一个电子表格),另一个是def按钮(基本上是为了保存我创建的所有按钮)。我正在使用tkintertable.Tables.TableCanvas保存功能制作一个保存表按钮,制作一个pickle文件。对于我的生活,我无法弄清楚如何将按钮链接到define函数中的表类。
我得到的错误是语法无效或没有属性。
任何有关如何解决这个问题的帮助都很棒
class AccuracyAssessmentApp:
def __init__(self, parent=Tk()):
self.mainWindow = (parent)
self.mainWindow.title("Accuracy Assessment")
self.mainWindow.geometry("1000x700")
self.make_txt()
self.pos_lbl()
self.buttons()
self.class_Window()
self.title_label()
def make_txt(self):
self.text = Text(self.mainWindow, width = 80, height = 40, background = "#A8A8A8")
self.text.pack(expand = TRUE, fill = BOTH)
def pos_lbl(self):
self.pos = Label(self.mainWindow, text = "0,0")
self.pos.place(x=0, y=0)
self.pos1 = Label(self.mainWindow, text = "0,350")
self.pos1.place(x=0, y=350)
self.pos2 = Label(self.mainWindow, text = "500,0")
self.pos2.place(x=500, y=0)
def class_Window(self):
self.tableFrame = Frame()
self.tableFrame.place(x=680,y=200)
self.table = TableCanvas(self.tableFrame, rows=30,cols=2, width=240)
self.table.createTableFrame()
def title_label(self):
self.titleFrame = Frame(self.mainWindow, bg= '#4A766E', relief="raised", bd=10)
self.titleFrame.place(x=385, y=20)
self.title = Label(self.titleFrame, text=("""Accuracy Assessment"""), font=LARGE_FONT, bg= '#4A766E', fg="white", justify="center")
self.title.pack(pady=10, padx=10)
def buttons(self):
Button(self.mainWindow, text="Quit", command=self.mainWindow.destroy).place(x=10,y=665)
Button(self.mainWindow, text="Save", command=self.table.save("AssessmentTable"))
这是我得到的错误:
runfile('I:/Custom_Scripts/Personal/AccuracyAssessment/AccuracyAssessment_v2.py', wdir='I:/Custom_Scripts/Personal/AccuracyAssessment')
Traceback (most recent call last):
File "<ipython-input-1-d9c198c0cd24>", line 1, in <module>
runfile('I:/Custom_Scripts/Personal/AccuracyAssessment/AccuracyAssessment_v2.py', wdir='I:/Custom_Scripts/Personal/AccuracyAssessment')
File "C:\Users\jpc08005\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Users\jpc08005\AppData\Local\Continuum\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "I:/Custom_Scripts/Personal/AccuracyAssessment/AccuracyAssessment_v2.py", line 79, in <module>
app = AccuracyAssessmentApp()
File "I:/Custom_Scripts/Personal/AccuracyAssessment/AccuracyAssessment_v2.py", line 41, in __init__
self.buttons()
File "I:/Custom_Scripts/Personal/AccuracyAssessment/AccuracyAssessment_v2.py", line 75, in buttons
Button(self.mainWindow, text="Save", command=self.table.save("AssessmentTable"))
AttributeError: AccuracyAssessmentApp instance has no attribute 'table'
答案 0 :(得分:0)
考虑以下代码:
Button(..., command=self.table.save("AssessmentTable"))
您在创建按钮时立即呼叫self.table.save("AssessmentTable")
。在创建self.table
之前调用此方法。因此,您会收到属性错误。
单击按钮时,只需调用self.table.save
即可。最简单的解决方案是创建一个保存表的函数,然后在回调中引用该按钮:
def save_table(self):
self.table.save("AssessmentTable")
def buttons(self):
Button(..., command=self.save_table)