如何使用Tkinter在屏幕的每个上角分组两组按钮?

时间:2018-12-10 14:43:22

标签: python user-interface tkinter tkinter-layout

我正在尝试编写一个GUI,该GUI将在窗口的左上角显示三个按钮,并在窗口的右上角显示四个按钮。我试图通过创建不同的框架来做到这一点:在“左”框架中,我将放置按钮组中的第一个按钮,然后在其后添加更多按钮。我在“ TopLeft corner”框架中添加了第一组的其余两个按钮。我适合第二组的前三个按钮的“右上角”框架。最后是一个“正确的”框架,在该框架中我适合最后一个按钮,以后将在其中添加更多按钮,如随附的图像所示。

但是问题是,左上角框架和右上角框架相互冲突并堆叠在一起,另外,右上角框架也堆叠在右框架之上,有人能解决此问题吗?

这是当前的GUI: This is the current GUI

这是我想要实现的目标: This is what i would like to achieve

from tkinter import *
import tkinter as tk
from tkinter import messagebox

win = tk.Tk()
width_value = win.winfo_screenwidth()
height_value = win.winfo_screenheight()
win.geometry(f"{width_value}x{height_value}+0+0")
win.resizable(False, True)
win.title("Test GUI")

leftFrame = tk.Frame(win)
leftFrame.pack(side=LEFT, anchor=N)

homeImg = PhotoImage(file="home_icon.png")
homeb = Button(leftFrame, image=homeImg, command=homeMenu, height=100, 
width=100).pack(padx=10, pady=10)

topLeftFrame = tk.Frame(win, width=200, height=100)
topLeftFrame.pack(side=TOP, anchor=W)

rgbmenuImg = PhotoImage(file="rgb_icon.png")
rgbmenub = Button(topLeftFrame, image=rgbmenuImg, command=rgbmenu, height=100, width=100)

thermalmenuImg = PhotoImage(file="thermal_icon.png")
cameraImg = PhotoImage(file="camera_icon.png")
thermalmenub = Button(topLeftFrame, image=thermalmenuImg, command=thermalmenu, height=100, width=100)

rgbmenub.grid(row=0, column=2, padx=10, pady=10)
thermalmenub.grid(row=0, column=3, padx=10, pady=10)

topRightFrame = tk.Frame(win, width=300, height=100)
topRightFrame.pack(side=TOP, anchor=E)

settImg = PhotoImage(file="settings_icon.png")
settingb = Button(topRightFrame, image=settImg, command=settings, height=100, width=100)

infoImg = PhotoImage(file="copyright_icon.png")
infob = Button(topRightFrame, image=infoImg, command=copyright, height=100, width=100)

loginImg = PhotoImage(file="login_icon.png")
loginb = Button(topRightFrame, image=loginImg, command=login, height=100, width=100)

settingb.grid(row=0, column=1, padx=10, pady=10)
infob.grid(row=0, column=2, padx=10, pady=10)
loginb.grid(row=0, column=3,padx=10, pady=10)

rightFrame = tk.Frame(win)
rightFrame.pack(side=RIGHT, anchor=N)

exitImg = PhotoImage(file="exit_icon.png")
exitb = Button(rightFrame, image=exitImg, command=quit, height=100, width=100).pack(padx=10, pady=10)

dema = Button(rightFrame, text="DEMA Intranet", command=dema_intranet).pack(padx=10, pady=10)

win.mainloop()

1 个答案:

答案 0 :(得分:1)

如果使用grid()几何图形管理器会更好,因为您可以更好地控制小部件的位置。使用pack(),您可以轻松弄乱一切。请注意,我为您划定了界限,以使我意识到我已经改变了一些东西。您可以在图片中清楚地看到框架。

import tkinter as tk
from tkinter import messagebox

win = tk.Tk()
width_value = win.winfo_screenwidth()
height_value = win.winfo_screenheight()
win.geometry(f"{width_value}x{height_value}+0+0")
win.resizable(False, True)
win.title("Test GUI")

win.grid_columnconfigure(0, weight=1)
win.grid_columnconfigure(1, weight=1)

topLeftFrame = tk.Frame(win, relief='solid', bd=2)
topLeftFrame.grid(row=0, column=0, padx=10, sticky="w")

homeImg = tk.PhotoImage(file="ex1.png")
homeb = tk.Button(topLeftFrame, image=homeImg, height=100, width=100).grid(row=0, column=0, padx=10, pady=10)

rgbmenuImg = tk.PhotoImage(file="ex1.png")
rgbmenub = tk.Button(topLeftFrame, image=rgbmenuImg, height=100, width=100)

thermalmenuImg = tk.PhotoImage(file="ex1.png")
cameraImg = tk.PhotoImage(file="ex1.png")
thermalmenub = tk.Button(topLeftFrame, image=thermalmenuImg, height=100, width=100)

rgbmenub.grid(row=0, column=1, padx=10, pady=10)
thermalmenub.grid(row=0, column=2, padx=10, pady=10)

topRightFrame = tk.Frame(win, relief='solid', bd=2)
topRightFrame.grid(row=0, column=1, padx=10, sticky="e")

settImg = tk.PhotoImage(file="ex1.png")
settingb = tk.Button(topRightFrame, image=settImg, height=100, width=100)

infoImg = tk.PhotoImage(file="ex1.png")
infob = tk.Button(topRightFrame, image=infoImg, height=100, width=100)

loginImg = tk.PhotoImage(file="ex1.png")
loginb = tk.Button(topRightFrame, image=loginImg, height=100, width=100)

settingb.grid(row=0, column=0, padx=10, pady=10)
infob.grid(row=0, column=1, padx=10, pady=10)
loginb.grid(row=0, column=2,padx=10, pady=10)

exitImg = tk.PhotoImage(file="ex1.png")
exitb = tk.Button(topRightFrame, image=exitImg, command=quit, height=100, width=100).grid(row=0, column=3, padx=10, pady=10)

leftFrame = tk.Frame(win, relief='solid', bd=2)
leftFrame.grid(row=1, column=0, padx=10, pady=10, sticky="nw")
tk.Button(leftFrame, text="Example 1").grid(row=1, column=0, pady=5)
tk.Button(leftFrame, text="Example 2").grid(row=2, column=0, pady=5)
tk.Button(leftFrame, text="Example 3").grid(row=3, column=0, pady=5)

rightFrame = tk.Frame(win, relief='solid', bd=2)
rightFrame.grid(row=1, column=1, padx=10, pady=10, sticky="ne")

dema = tk.Button(rightFrame, text="DEMA Intranet")
dema.grid(row=0, column=0, pady=5)
tk.Button(rightFrame, text="Example 4").grid(row=1, column=0, pady=5)
tk.Button(rightFrame, text="Example 5").grid(row=2, column=0, pady=5)
tk.Button(rightFrame, text="Example 6").grid(row=3, column=0, pady=5)

win.mainloop()

enter image description here

P.S。我只有一个50x50的“主页”图标。