【问题标题】:tkinter useing grid on a frame doesn't seem to worktkinter 在框架上使用网格似乎不起作用
【发布时间】:2020-12-14 17:06:45
【问题描述】:

我试图获得一个 tkinter gui,让用户登录我对 tkinter 真的很陌生。我制作了一个框架,当我使用网格将另一个框架小部件放在框架内时,它基于根而不是我认为正在发生的 inner_frame。在代码中,我制作了一个灰色框进行演示,但我不明白为什么它位于蓝色框下方而不是“登录”文本下方的黄色框内。感谢您的帮助。

from tkinter import *
root = Tk()
root.title("sighn in test")

#colors
background = "#273E47"
accent = "#d8973c"
red = "#bb4430"
white = "#edf2f4"

#this creates and places the background frame
main_buttons_frame = Frame(root, height = 500, width = 400, bg = background).grid(row = 0, column = 0)
#this creates and places the inner frame
inner_frame = Frame(main_buttons_frame, height = 450, width = 300, bg = accent).grid(row = 0, column = 0)
#this creates and places the "sighn in text"
top_text = Label(inner_frame, text = "sign in", font = ("helvitica", 30, "bold"), bg = accent, fg =         
background).grid(row = 0, column = 0)
#this is a test to demonstrate
test_frame = Frame(inner_frame, bg = "grey", height = 100, width = 100).grid(row = 1, column = 0)


root.mainloop()

【问题讨论】:

    标签: python user-interface tkinter grid frames


    【解决方案1】:

    初学者常犯的错误

    inner_frame = Frame(...).grid...()
    

    它将None 分配给inner_frame,因为grid()/pack()/place() 给出None

    所以稍后Frame(inner_frame, ..) 表示Frame(None, ..) 并添加到root

    你必须分两步完成

    inner_frame = Frame(...)
    inner_frame.grid(...)
    

    现在您已将Frame 分配给inner_frame


    编辑:

    我得到正确分配的小部件

    现在灰色框在黄色框架内,但图像显示不同的问题 - grid()/pack() 自动计算位置和大小,外部框架自动更改大小以适合孩子的大小。

    使用.grid_propagate(False) 可以阻止它

    但它显示了其他问题 - 单元格不使用父级的完整大小,因此黄色框架移动到左上角,而不是居中 :) 空单元格有 width = 0heigh = 0 所以全部移动到下一行和下一行列不会改变它 - 它仍然在左上角:)

    您需要将weight 分配给决定如何使用可用空间的列和/或行。所有列/行都有weight=0,当您设置weight=1 时,此行和/或列将使用所有可用空间-(这需要更好的解释)-然后单元格内的元素将居中。

     main_buttons_frame.grid_columnconfigure(0, weight=1)
     main_buttons_frame.grid_rowconfigure(0, weight=1)
    


    import tkinter as tk # PEP8: `import *` is not preferred
    
    # --- colors ---
    
    background = "#273E47"
    accent = "#d8973c"
    red = "#bb4430"
    white = "#edf2f4"
    
    # --- main ---
    
    root = tk.Tk()
    root.title("sighn in test")
    
    main_buttons_frame = tk.Frame(root, height=500, width=400, bg=background) # PEP8: without spaces around `=` inside `( )`
    main_buttons_frame.grid(row=0, column=0)
    #main_buttons_frame = None
    main_buttons_frame.grid_propagate(False)
    main_buttons_frame.grid_columnconfigure(0, weight=1)
    main_buttons_frame.grid_rowconfigure(0, weight=1)
    
    inner_frame = tk.Frame(main_buttons_frame, height=450, width=300, bg=accent)
    inner_frame.grid(row=0, column=0)
    #inner_frame = None
    inner_frame.grid_propagate(False)
    inner_frame.grid_columnconfigure(0, weight=1)
    inner_frame.grid_rowconfigure(0, weight=1)
    
    top_text = tk.Label(inner_frame, text="sign in", font=("helvitica", 30, "bold"), bg=accent, fg=background)
    top_text.grid(row=0, column=0,)
    #top_text = None
    #top_text.grid_propagate(False)
    #top_text.grid_columnconfigure(0, weight=1)
    #top_text.grid_rowconfigure(0, weight=1)
    
    test_frame = tk.Frame(inner_frame, bg="grey", height=100, width=100)
    test_frame.grid(row=1, column=0)
    #test_frame = None
    #test_frame.grid_propagate(False)
    #test_frame.grid_columnconfigure(1, weight=1)
    #test_frame.grid_rowconfigure(0, weight=1)
    
    root.mainloop()
    

    顺便说一句: PEP 8 -- Style Guide for Python Code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 2011-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多