我是 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()
最佳答案
您没有使用 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)
关于python - Tkinter:在网格布局中获取按钮上方的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864353/