Tkinter - 如何动态调整渐变框架的大小?
Tkinter - How to dynamically resize gradient frames?
我想知道如何根据 window 大小自动调整以下渐变框的大小。
from tkinter import *
class InventorySystem:
def __init__(self, root):
self.root = root
self.root.title("Inventory System")
self.root.geometry("1366x768")
j = 0
colors = ["#007F5F", "#2B9348", "#55A630", "#80B918", "#AACC00", "#BFD200",
"#D4D700", "#DDDF00", "#EEEF20", "#FFFF3F"]
for color in colors:
Frame(self.root, width = 1366 / len(colors), height = 768,
bg = color).place(x = j, y = 0)
j = j + 1366 / len(colors)
if __name__ == "__main__":
root = Tk()
application = InventorySystem(root)
root.mainloop()
你想做的事情有点复杂,所以即使你不只是学习如何使用 tkinter
也可能具有挑战性(只是因为它的文档太少而加剧了这一点)。因此,我创建了一个示例,说明如何根据您问题中的代码执行此操作。
一个不直观的事情(对我来说)是 Event
处理 window 的 <'Configure>'
事件的“回调”函数被调用 window 本身 以及其中的所有小部件 — 如果您没有意识到会发生什么,这可能 非常 令人困惑。
我还更改并重新格式化了您的代码以更严格地遵循 PEP 8 - Style Guide for Python Code suggestions, which I strongly suggest your read and start following. Note in particular I changed your wildcard from tkinter import *
into the preferrable import tkinter as tk
— see Should wildcard import be avoided?
import tkinter as tk
WIDTH, HEIGHT = 1366, 768 # Initial size of root window.
class InventorySystem:
# Gradient frame colors.
COLORS = ["#007F5F", "#2B9348", "#55A630", "#80B918", "#AACC00",
"#BFD200", "#D4D700", "#DDDF00", "#EEEF20", "#FFFF3F"]
def __init__(self, root):
self.root = root
self.root.title("Inventory System")
self.root.geometry(f'{WIDTH}x{HEIGHT}')
self.width, self.height = WIDTH, HEIGHT # Size of root window.
self.create_gradient_frames()
def create_gradient_frames(self):
"""Create a gradient frame for each color."""
self.gradient_frames = []
colors = self.COLORS # Alias for local access.
gradient_frame_width = round(self.width / len(colors))
gradient_frame_height = self.height
for i, color in enumerate(colors):
frame = tk.Frame(self.root, width=gradient_frame_width,
height=gradient_frame_height, bg=color)
frame.place(x=i * gradient_frame_width,
y=self.height - gradient_frame_height)
self.gradient_frames.append(frame)
def update_gradient_frames(self):
"""Update size and position of all gradient frames."""
gradient_frame_width = round(self.width / len(self.COLORS))
gradient_frame_height = self.height
for i, frame in enumerate(self.gradient_frames):
frame.config(width=gradient_frame_width, height=gradient_frame_height)
frame.place(x=i * gradient_frame_width,
y=self.height - gradient_frame_height)
def on_resize(self, event):
"""Root window size change event handler."""
# Needed following because event handler is called for the container win
# and all its child widgets but we only need to handle container itself.
if event.widget.master is not None: # Not the root window?
return # Ignore.
width, height = root.winfo_width(), root.winfo_height() # Current size.
if width != self.width or height != self.height: # Updated?
self.width, self.height = width, height # Track change.
self.update_gradient_frames()
if __name__ == "__main__":
root = tk.Tk()
application = InventorySystem(root)
root.bind('<Configure>', application.on_resize) # Bind callback function to event.
root.mainloop()
这是调整大小的 window 的屏幕截图:
您可以使用相对选项 of .place()
:
# relative width of each frame
# 1.0 means same width of parent
relw = 1 / len(colors)
for j, color in enumerate(colors):
Frame(self.root, bg=color).place(relx=j*relw, y=0, relwidth=relw, relheight=1)
详见place()
官方文档
我想知道如何根据 window 大小自动调整以下渐变框的大小。
from tkinter import *
class InventorySystem:
def __init__(self, root):
self.root = root
self.root.title("Inventory System")
self.root.geometry("1366x768")
j = 0
colors = ["#007F5F", "#2B9348", "#55A630", "#80B918", "#AACC00", "#BFD200",
"#D4D700", "#DDDF00", "#EEEF20", "#FFFF3F"]
for color in colors:
Frame(self.root, width = 1366 / len(colors), height = 768,
bg = color).place(x = j, y = 0)
j = j + 1366 / len(colors)
if __name__ == "__main__":
root = Tk()
application = InventorySystem(root)
root.mainloop()
你想做的事情有点复杂,所以即使你不只是学习如何使用 tkinter
也可能具有挑战性(只是因为它的文档太少而加剧了这一点)。因此,我创建了一个示例,说明如何根据您问题中的代码执行此操作。
一个不直观的事情(对我来说)是 Event
处理 window 的 <'Configure>'
事件的“回调”函数被调用 window 本身 以及其中的所有小部件 — 如果您没有意识到会发生什么,这可能 非常 令人困惑。
我还更改并重新格式化了您的代码以更严格地遵循 PEP 8 - Style Guide for Python Code suggestions, which I strongly suggest your read and start following. Note in particular I changed your wildcard from tkinter import *
into the preferrable import tkinter as tk
— see Should wildcard import be avoided?
import tkinter as tk
WIDTH, HEIGHT = 1366, 768 # Initial size of root window.
class InventorySystem:
# Gradient frame colors.
COLORS = ["#007F5F", "#2B9348", "#55A630", "#80B918", "#AACC00",
"#BFD200", "#D4D700", "#DDDF00", "#EEEF20", "#FFFF3F"]
def __init__(self, root):
self.root = root
self.root.title("Inventory System")
self.root.geometry(f'{WIDTH}x{HEIGHT}')
self.width, self.height = WIDTH, HEIGHT # Size of root window.
self.create_gradient_frames()
def create_gradient_frames(self):
"""Create a gradient frame for each color."""
self.gradient_frames = []
colors = self.COLORS # Alias for local access.
gradient_frame_width = round(self.width / len(colors))
gradient_frame_height = self.height
for i, color in enumerate(colors):
frame = tk.Frame(self.root, width=gradient_frame_width,
height=gradient_frame_height, bg=color)
frame.place(x=i * gradient_frame_width,
y=self.height - gradient_frame_height)
self.gradient_frames.append(frame)
def update_gradient_frames(self):
"""Update size and position of all gradient frames."""
gradient_frame_width = round(self.width / len(self.COLORS))
gradient_frame_height = self.height
for i, frame in enumerate(self.gradient_frames):
frame.config(width=gradient_frame_width, height=gradient_frame_height)
frame.place(x=i * gradient_frame_width,
y=self.height - gradient_frame_height)
def on_resize(self, event):
"""Root window size change event handler."""
# Needed following because event handler is called for the container win
# and all its child widgets but we only need to handle container itself.
if event.widget.master is not None: # Not the root window?
return # Ignore.
width, height = root.winfo_width(), root.winfo_height() # Current size.
if width != self.width or height != self.height: # Updated?
self.width, self.height = width, height # Track change.
self.update_gradient_frames()
if __name__ == "__main__":
root = tk.Tk()
application = InventorySystem(root)
root.bind('<Configure>', application.on_resize) # Bind callback function to event.
root.mainloop()
这是调整大小的 window 的屏幕截图:
您可以使用相对选项 of .place()
:
# relative width of each frame
# 1.0 means same width of parent
relw = 1 / len(colors)
for j, color in enumerate(colors):
Frame(self.root, bg=color).place(relx=j*relw, y=0, relwidth=relw, relheight=1)
详见place()
官方文档