【问题标题】:Resizing tkinter widgets in an application在应用程序中调整 tkinter 小部件的大小
【发布时间】:2022-01-03 17:13:18
【问题描述】:

我正在尝试使用 tkinter 构建一个导航器应用程序,并且对制作 GUI 和使用 tkinter 非常陌生。当调整整个窗口的大小时,我似乎无法弄清楚如何很好地调整小部件的大小。目前调整大小的工作方式如下图所示。

顶部的描述是一个 tkinter Message 小部件,中间的导航树是一个 Checklist,两个按钮是常规 Button,而 Buttons 下面的文本是一个 Label 对象。我使用 .grid() 方法将它们放在 tkinter 根目录中,并根据需要使用 ipadx、ipady、padx 和 pady 来调整它们的大小。

如果窗口大小与 Checklist 对象有些相似,我希望文本和按钮不会消失。理想情况下,在调整窗口大小时,我希望文本自动换行并为自己腾出空间。我可以为窗口设置一个最小尺寸,以确保一切都合适,但调整大小仍然不够优雅。我似乎也无法弄清楚自动换行。

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    这是示例代码,可帮助您了解调整大小的工作原理。 我建议你使用Grid.rowconfigure()Grid.columnconfigure()

    示例代码:

    import tkinter as tk
    from tkinter import Grid, Button
    
    root = tk.Tk()
    root.title("resize button")
    root.geometry("500x500")
    
    
    # here you need to put on what do you want to use row configure, index(row) and weight
    Grid.rowconfigure(root, 0, weight=1)  # we use on root, row=0 weight=1
    Grid.columnconfigure(root, 0, weight=1)
    
    #configure 2nd row
    Grid.rowconfigure(root, 1, weight=1)
    
    
    #configure 3rd row
    Grid.rowconfigure(root, 2, weight=1)
    
    #configure 2nd column
    Grid.columnconfigure(root, 1, weight=1)
    
    button1 = Button(root, text="Button1")
    button2 = Button(root, text="Button2")
    button3 = Button(root, text="Button3")
    
    button1.grid(row=0, column=0, sticky="nsew")
    button2.grid(row=1, column=0, sticky="nsew")
    button3.grid(row=2, column=0, sticky="nsew")
    
    button1_1 = Button(root, text="Button1_1")
    button2_1 = Button(root, text="Button2_1")
    button3_1 = Button(root, text="Button3_1")
    
    button1_1.grid(row=0, column=1, sticky="nsew")
    button2_1.grid(row=1, column=1, sticky="nsew")
    button3_1.grid(row=2, column=1, sticky="nsew")
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 1970-01-01
      • 2022-10-08
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2017-10-18
      • 2014-09-01
      • 1970-01-01
      相关资源
      最近更新 更多