简体   繁体   English

tkinter 如何独立于左侧小部件的长度将小部件右对齐?

[英]tkinter how to align widgets to the right independently of the length of widgets to the left?

I would like to align the entry widgets to the right, independently of the width of the labels to the left.我想将条目小部件向右对齐,而与左侧标签的宽度无关。 Id the width of the titles should not matter. id 标题的宽度应该无关紧要。 The entry widgets should align no matter what the width of the titles are.无论标题的宽度是多少,条目小部件都应该对齐。

在此处输入图像描述

Here is my code:这是我的代码:

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

I have tried using sticky="e" but nothing changes.我试过使用sticky="e"但没有任何变化。 I don't want to use pack(side='right') because it will look messy/ugly if the window width increases.我不想使用pack(side='right')因为如果 window 宽度增加,它看起来会很乱/很难看。

The reason sticky = 'e' does not work in this case is because you have not used weight . sticky = 'e' 在这种情况下不起作用的原因是因为您没有使用weight If you add a weight to the column on the left or right (or both), your problem should be solved.如果您向左侧或右侧(或两者)的列添加weight ,则您的问题应该得到解决。

What is weight ?什么是weight

The default weight for any row or column is 0. If you don't give a row or column any weight , it will just take the required space to accommodate the widgets inside.任何行或列的默认weight都是 0。如果你不给行或列任何weight ,它只会占用所需的空间来容纳里面的小部件。 But if you give the column a weight , you are asking the column to take up as much space as possible.但是如果你给列一个weight ,你就是在要求列占用尽可能多的空间。

Intuition: Think of the available space in a frame like a rectangular elastic sheet.直觉:将框架中的可用空间想象成矩形弹性板。 By adding the desired weights to different rows and columns, you can proportionately make the sheet expand in the right areas.通过将所需的权重添加到不同的行和列,您可以按比例使工作表在正确的区域扩展。

Adding weights to rows and columns:为行和列添加weights

<containter-widget>.grid_columnconfigure(<column number>, weight = <whole number>)

<containter-widget>.grid_rowconfigure(<row number>, weight = <whole number>)

Run this code to understand weights better:运行此代码以更好地理解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()

Try tweaking the values of the weights and see what changes happen.尝试调整weights的值,看看会发生什么变化。

You must permit to column 0 of frames to fill the frame size: with a foo1_frame.columnconfigure(0, weight=1)您必须允许帧的第 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()

I came with a bit different answer, hope it's works for you, here is the code:我给出了一些不同的答案,希望它对你有用,这里是代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM