2016-04-26 92 views
0

所以我對tKinter很陌生,以及在python中使用類來達到我在這裏使用它們的程度。我在這個話題上做了大量的研究,但我很難把它放在一起。問題是;我有我的主要類下的def函數,第一個是def class_Window(它創建一個電子表格),另一個是def按鈕(實質上是保存我創建的所有按鈕)。我正在使用tkintertable.Tables.TableCanvas保存函數來製作保存表按鈕的過程,並生成一個pickle文件。對於我的生活,我無法弄清楚如何將按鈕鏈接到定義函數中的表類。跨類連接tKinter按鈕

我得到的錯誤是無效的語法或沒有屬性。

如何得到這個跨來的任何幫助將是巨大的

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

請顯示一個實際的錯誤。錯誤和回溯具有描述問題出現的具體信息,以及問題是什麼。 –

+1

請不要在評論中。你可以編輯你的問題。 –

回答

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) 
+0

謝謝。它工作完美。對於課程而言,新的課程仍然是一個很好的解決方案,可以看出它是如何通過的 – Jake