【问题标题】:tkinter how to align widgets to the right independently of the length of widgets to the left?tkinter 如何将小部件向右对齐,而与左侧小部件的长度无关?
【发布时间】:2022-02-18 19:19:03
【问题描述】:

我想将条目小部件向右对齐,与左侧标签的宽度无关。 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'),因为如果窗口宽度增加,它会显得凌乱/丑陋。

【问题讨论】:

标签: python tkinter


【解决方案1】:

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 的值,看看会发生什么变化。

【讨论】:

    【解决方案2】:

    您必须允许第 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()
    

    【讨论】:

      【解决方案3】:

      我的答案有点不同,希望对你有用,代码如下:

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

      【讨论】:

        猜你喜欢
        • 2020-07-30
        • 1970-01-01
        • 2021-07-23
        • 2020-10-04
        • 2020-12-19
        • 2021-03-13
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多