2013-04-07 140 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