【问题标题】:Tkinter: Getting an image above buttons in grid layoutTkinter:在网格布局中的按钮上方获取图像
【发布时间】:2013-03-29 15:56:48
【问题描述】:

我是 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()  

【问题讨论】:

    标签: python layout user-interface tkinter


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 2014-03-31
      • 1970-01-01
      相关资源
      最近更新 更多