在 Tkinter 中调整大小时缩小小部件(我可以决定缩小行为吗?)
Shrinking widgets during resize in Tkinter (can I decide shrinkage behavior?)
我正在 Python/Tkinter 中设计 GUI。调整根框架的大小使几何控制器根据可用房间调整 GUI 中的小部件并调整其大小,到这里一切都很好。
但是...首先缩小的小部件之一是左对齐标签。我希望它从右侧开始消失,而不是 Tkinter 同时从左侧和右侧减少它。
这里是复制这种情况的示例代码:
import tkinter as tk
root = tk.Tk()
my_font = ("Consolas", 12)
label1 = tk.Label(root, text="Label 1\nGoes on the Left", justify="left", font=my_font, bg='cyan')
label2 = tk.Label(root, text="Label 2\nGoes on the Right", justify="left", font=my_font, bg='magenta')
label3 = tk.Label(root, text="Label 3 - Goes in the middle\n(During a resize, this shrinks first)",
justify="left", font=my_font, bg='orange')
label1.pack(side=tk.LEFT)
label2.pack(side=tk.RIGHT)
label3.pack(side=tk.LEFT) # packed last, so it gets reduced first during a resize
root.mainloop()
下面是同一代码所发生情况的图片:resize in action
有没有办法告诉几何控制器如何在 window 调整大小时减少小部件?
非常感谢。
答案是对label3进行简单的配置。添加 anchor='w' 属性,它将按照您描述的方式运行。标签只会从右边收缩。
label3 = tk.Label(root, text="Label 3 - Goes in the middle\n(During a resize, this shrinks first)", justify="left", font=my_font, bg='orange', anchor='w')
我正在 Python/Tkinter 中设计 GUI。调整根框架的大小使几何控制器根据可用房间调整 GUI 中的小部件并调整其大小,到这里一切都很好。
但是...首先缩小的小部件之一是左对齐标签。我希望它从右侧开始消失,而不是 Tkinter 同时从左侧和右侧减少它。
这里是复制这种情况的示例代码:
import tkinter as tk
root = tk.Tk()
my_font = ("Consolas", 12)
label1 = tk.Label(root, text="Label 1\nGoes on the Left", justify="left", font=my_font, bg='cyan')
label2 = tk.Label(root, text="Label 2\nGoes on the Right", justify="left", font=my_font, bg='magenta')
label3 = tk.Label(root, text="Label 3 - Goes in the middle\n(During a resize, this shrinks first)",
justify="left", font=my_font, bg='orange')
label1.pack(side=tk.LEFT)
label2.pack(side=tk.RIGHT)
label3.pack(side=tk.LEFT) # packed last, so it gets reduced first during a resize
root.mainloop()
下面是同一代码所发生情况的图片:resize in action
有没有办法告诉几何控制器如何在 window 调整大小时减少小部件?
非常感谢。
答案是对label3进行简单的配置。添加 anchor='w' 属性,它将按照您描述的方式运行。标签只会从右边收缩。
label3 = tk.Label(root, text="Label 3 - Goes in the middle\n(During a resize, this shrinks first)", justify="left", font=my_font, bg='orange', anchor='w')