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

How to group two sets of buttons on each top corner of the screen using Tkinter?

我正在尝试编写一个 GUI,它将在 window 的左上角显示三个按钮,在右上角显示四个按钮。我试图通过创建不同的框架来做到这一点:一个 "left" 框架,我在其中放置了一组按钮中的第一个,稍后我将在其中添加更多按钮。 "TopLeft corner" 框架,我在其中添加第一组的其余两个按钮。我适合第二组的前三个按钮的 "Topright corner" 框架。最后是一个 "right" 框架,我在其中放置最后一个按钮,稍后我将在其中添加更多按钮,如所附图片所示。

但问题是左上角和右上角的框架冲突并相互堆叠,另外右上角的框架堆叠在右框架的顶部,有人解决这个问题吗?

这是当前的图形用户界面:

这就是我想要实现的:

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

如果您使用 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()

P.S。我只有一个 50x50 "home" 图标。