[英]tkinter how to align widgets to the right independently of the length of widgets to the left?
我想将条目小部件向右对齐,而与左侧标签的宽度无关。 id 标题的宽度应该无关紧要。 无论标题的宽度是多少,条目小部件都应该对齐。
这是我的代码:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('test')
root.geometry("500x800")
# main frame
main_frame = tk.Frame(root)
main_frame.pack(expand=1, fill='both')
# the message
title = tk.Label(main_frame, text="title 1", bg='white')
title.pack(padx=8, pady=8, fill='x')
foo1_frame = tk.Frame(main_frame)
foo1_frame.pack(padx=8, pady=8, fill='x')
foo1_label = tk.Label(foo1_frame, text="short title foo1")
foo1_label.grid(row=0, column=0)
foo1_entry_box = tk.Entry(foo1_frame, width=10)
foo1_entry_box.grid(row=0, column=2, sticky='e') #sticky does nothing in this case
foo2_frame = tk.Frame(main_frame)
foo2_frame.pack(padx=8, pady=8, fill='x')
foo2_label = tk.Label(foo2_frame, text="loooooooooooonger title foo2")
foo2_label.grid(row=1, column=0)
foo2_entry_box = tk.Entry(foo2_frame, width=10)
foo2_entry_box.grid(row=1, column=2)
root.mainloop()
我试过使用sticky="e"
但没有任何变化。 我不想使用pack(side='right')
因为如果 window 宽度增加,它看起来会很乱/很难看。
sticky = 'e' 在这种情况下不起作用的原因是因为您没有使用weight
。 如果您向左侧或右侧(或两者)的列添加weight
,则您的问题应该得到解决。
weight
? 任何行或列的默认weight
都是 0。如果你不给行或列任何weight
,它只会占用所需的空间来容纳里面的小部件。 但是如果你给列一个weight
,你就是在要求列占用尽可能多的空间。
直觉:将框架中的可用空间想象成矩形弹性板。 通过将所需的权重添加到不同的行和列,您可以按比例使工作表在正确的区域扩展。
weights
:<containter-widget>.grid_columnconfigure(<column number>, weight = <whole number>)
<containter-widget>.grid_rowconfigure(<row number>, weight = <whole number>)
运行此代码以更好地理解weights
:
from tkinter import *
root = Tk()
#Gridding root column-wise
root.grid_columnconfigure(0, weight = 2)
root.grid_columnconfigure(1, weight = 1)
#Gridding root row-wise
root.grid_rowconfigure(0, weight = 1)
frame_left = Frame(root, bg = "blue")
frame_right = Frame(root, bg = "green")
frame_left.grid(row = 0, column = 0, sticky = NSEW)
frame_right.grid(row = 0, column = 1, sticky = NSEW)
root.mainloop()
尝试调整weights
的值,看看会发生什么变化。
您必须允许帧的第 0 列填充帧大小:使用foo1_frame.columnconfigure(0, weight=1)
root = tk.Tk()
root.title('test')
root.geometry("500x800")
# main frame
main_frame = tk.Frame(root)
main_frame.pack(expand=1, fill='both')
# the message
title = tk.Label(main_frame, text="title 1", bg='white')
title.pack(padx=8, pady=8, fill='x')
foo1_frame = tk.Frame(main_frame)
foo1_frame.pack(padx=8, pady=8, fill='x')
foo1_frame.columnconfigure(0, weight=1) # expand column 0
foo1_label = tk.Label(foo1_frame, text="short title foo1")
foo1_label.grid(row=0, column=0, sticky='w') # sticky='w'
foo1_entry_box = tk.Entry(foo1_frame, width=10)
foo1_entry_box.grid(row=0, column=2, sticky='e') # sticky='e'
foo2_frame = tk.Frame(main_frame)
foo2_frame.pack(padx=8, pady=8, fill='x')
foo2_frame.columnconfigure(0, weight=1) # expand column 0
foo2_label = tk.Label(foo2_frame, text="loooooooooooonger title foo2")
foo2_label.grid(row=1, column=0, sticky='w') # sticky='w'
foo2_entry_box = tk.Entry(foo2_frame, width=10)
foo2_entry_box.grid(row=1, column=2, sticky='e') # sticky='e'
root.mainloop()
我给出了一些不同的答案,希望它对你有用,这里是代码:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title('test')
root.geometry("500x800")
# main frame
# main_frame = tk.Frame(root)
# main_frame.pack(expand=1, fill='both')
# the message
title = tk.Label(root, text="title 1", bg='white')
title.place(relx = 0.0, rely=0.01, relwidth = 1.0)
# foo1_frame = tk.Frame(root)
# foo1_frame.pack(padx=8, pady=8, fill='x')
foo1_label = tk.Label(root, text="short title foo1")
foo1_label.place(relx = 0.1, rely = 0.05)
foo1_entry_box = tk.Entry(root, width=10)
foo1_entry_box.place(relx = 0.7, rely = 0.05) #sticky does nothing in this case
# foo2_frame = tk.Frame(root)
# foo2_frame.pack(padx=8, pady=8, fill='x')
foo2_label = tk.Label(root, text="loooooooooooonger title foo2")
foo2_label.place(relx = 0.1, rely = 0.1)
foo2_entry_box = tk.Entry(root, width=10)
foo2_entry_box.place(relx = 0.7, rely = 0.1)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.