2

我是 tkinter 的新手,我正在尝试制作一个 GUI,顶部有一个图像,该图像下方有 4 个按钮区域,这将是一种选择答案的方法。但是,到目前为止,我创建的代码似乎只停留在左上角,根本不会在图像下方移动,请问有人知道解决方案吗?

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()  
4

1 回答 1

3

您没有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)
于 2013-04-07T16:05:44.793 回答