2013-04-07 53 views
0

我是tkinter的新手,我試圖製作一個GUI,其中有一個圖像位於頂部,圖像下方有4個按鈕區域,這將是選擇答案。然而,到目前爲止,我所創建的按鈕代碼似乎只停留在左上角,根本不會在圖像下移動,有沒有人知道這個解決方案?Tkinter:在網格佈局中的按鈕上方獲取圖像

import Tkinter as tk 
from Tkinter import * 
from Tkinter import PhotoImage 

root = Tk() 

class Class1(Frame): 

    def __init__(self, master): 
     Frame.__init__(self, master) 
     self.grid() 

     self.master = master   
     self.question1_UI() 

    def question1_UI(self): 

     self.master.title("GUI")   

     gif1 = PhotoImage(file = 'Image.gif') 

     label1 = Label(image=gif1) 
     label1.image = gif1 
     label1.grid(row=1, column = 0, columnspan = 2, sticky=NW) 

     questionAButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
     questionAButton.grid(row = 2, column = 1, sticky = S) 
     questionBButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
     questionBButton.grid(row = 2, column = 2, sticky = S) 
     questionCButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
     questionCButton.grid(row = 3, column = 3, sticky = S) 
     questionDButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
     questionDButton.grid(row = 3, column = 4, sticky = S) 



def main(): 


    ex = Class1(root) 
    root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), 
    root.winfo_screenheight()))   
    root.mainloop() 


if __name__ == '__main__': 
    main() 

回答

1

您沒有使用self作爲label1父。此外,網格管理器從第0行開始:

def question1_UI(self): 
    # ... 
    label1 = Label(self, image=gif1) 
    label1.image = gif1 
    label1.grid(row = 0, column = 0, columnspan = 2, sticky=NW) 

    questionAButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
    questionAButton.grid(row = 1, column = 0, sticky = S) 
    questionBButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
    questionBButton.grid(row = 1, column = 1, sticky = S) 
    questionCButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
    questionCButton.grid(row = 2, column = 0, sticky = S) 
    questionDButton = Button(self, text='Submit',font=('MS', 8,'bold')) 
    questionDButton.grid(row = 2, column = 1, sticky = S) 
+0

非常感謝! – user2254822 2013-04-07 16:08:19