2015-02-11 24 views
1

在下面的代碼中,app是mainWindow的一個實例,它從Tkinter.Frame繼承。我試圖使用Frame.Configure方法來更改Frame的背景顏色。但是,調用self.configure(background =「yellow」)不起作用。有人能幫助我理解我正在犯的錯誤嗎?創建後無法更改框架顏色

import Tkinter 


class mainWindow(Tkinter.Frame): 
    def __init__(self, parent): 
     Tkinter.Frame.__init__(self, master=parent) 
     self.parent=parent 
     self.button1=Tkinter.Button(master=self.parent, text='ONE', command=self.change) 
     self.button1.pack() 
     self.pack() 

    def change(self): 
     self.parent.wm_title("Changed") 
     self.configure(background="yellow") 


root = Tkinter.Tk() 
root.geometry("600x600+50+50") 
app=mainWindow(root) 
root.mainloop() 

回答

0

嘗試self.parent.configure(background="yellow")

我是新來Tkinter(少數分鐘的新的),所以根據你的代碼我的猜測是,框架沒有顯示在所有。該框架的父項是root,它也是該按鈕的父項。在我的推理

基礎之上,和馬辛的答案,我推斷,框架只是簡單地沒有一個:

所以在這裏,我改變了根的(頂級窗口部件)的背景

編輯尺寸。所以這裏是你的代碼的一個編輯版本,擴展了框架,框架將包含按鈕。

import Tkinter 

class mainWindow(Tkinter.Frame): 
    def __init__(self, parent): 
     Tkinter.Frame.__init__(self, master=parent) 
     self.parent=parent 
     self.button1=Tkinter.Button(master=self, text='ONE', command=self.change) 
     self.button1.pack() 
     self.pack(fill=Tkinter.BOTH, expand=True) 

    def change(self): 
     self.parent.wm_title("Changed") 
     self.configure(background="yellow") 

root = Tkinter.Tk() 
root.geometry("600x600+50+50") 
app=mainWindow(root) 
root.mainloop() 
2

它不起作用,因爲你的框架「很小」。它不包含任何小部件(按鈕的父窗口是頂部窗口,而不是框架)。因此,爲了使框架大,因此可見,您需要擴大它:

import Tkinter 


class mainWindow(Tkinter.Frame): 
    def __init__(self, parent): 
     Tkinter.Frame.__init__(self, master=parent) 
     self.parent=parent 
     self.button1=Tkinter.Button(master=self.parent, 
            text='ONE', 
            command=self.change) 

     self.button1.pack() 
     self.pack(fill=Tkinter.BOTH, expand=1) #<--- expand frame 

    def change(self): 
     self.parent.wm_title("Changed") 
     self.configure(background="yellow") 


root = Tkinter.Tk() 
root.geometry("600x600+50+50") 
app=mainWindow(root) 

root.mainloop()