繁体   English   中英

tkinter 框架没有填充空白区域?

[英]tkinter frame not filling empty space?

我试图制作相同大小的列表框并将music_box_label扩展到空白区域。 在 list_box_frame 和list_box上尝试了grid(sticky = "nsw" ,在list_box_frame上尝试了grid(sticky = "nsew")以扩展到剩余大小。我也尝试给 label 赋予权重,但我没有工作。所以我做错了什么.

这是我的代码

from tkinter import *
root = tk.Tk()
root.title("RHYTHM")
photo = tk.PhotoImage(file = "image/mp3_logo.png")
root.iconphoto(False, photo)

class main_window():
    def __init__(self,root):
        self.root = root
        
        #creating label frame for buttons(top frame)
        self.label_frame1 = ttk.Frame(self.root)
        self.label_frame1.grid(row = 0,column = 0)
        
        self.music_box= ttk.LabelFrame(self.root,text = "Music box")
        self.music_box.grid(row = 1,column = 1,sticky = "nsew")
        Grid.rowconfigure(self.root,1,weight = 1)
        Grid.columnconfigure(self.root,1,weight = 1)
        
        self.list_box_frame = ttk.LabelFrame(self.root,text = "list box",width = 20)
        self.list_box_frame.grid(row = 1,column = 0,sticky = "nsw")
        Grid.rowconfigure(self.root,1,weight = 2)
        Grid.columnconfigure(self.root,0,weight = 2)
        
  
        
        #list box in first frame
        self.scroll_bar1 = ttk.Scrollbar(self.list_box_frame,orient = VERTICAL)
        self.scroll_bar1.grid(column = 1,sticky = "nsew")
        self.scroll_bar2 = ttk.Scrollbar(self.list_box_frame,orient = HORIZONTAL)
        self.scroll_bar2.grid(row = 1,sticky = "nsew")
        
        self.list_box = tk.Listbox(self.list_box_frame,yscrollcommand=self.scroll_bar1.set,xscrollcommand=self.scroll_bar2.set,width = 20)
        self.list_box.grid(row = 0,column = 0,sticky = "nsw")
        
        self.scroll_bar1.config(command = self.list_box.yview)
        self.scroll_bar2.config(command = self.list_box.xview)
   
        Grid.rowconfigure(self.list_box_frame,0,weight = 1)
        Grid.columnconfigure(self.list_box_frame,0,weight = 1)
        
        self.root.mainloop()
    

这是 output 图片以防万一

粗略地说,在使用grid时让一个 widget 填充所有可用空间需要两个步骤:

  1. 将行和/或列配置为具有正weight ,以便将额外空间分配给行或列,以及
  2. 使用sticky属性使小部件填充分配给它的空间。

专业提示:当所有对gridpackplace的调用组合在一起时,布局问题更容易可视化和调试。

这就是我会做的,假设你想要所有额外的空间到左边的 go。 如果您希望左侧和右侧(列表框、音乐框)都展开,那么您需要为第 1 列添加权重。

self.root.grid_rowconfigure(1, weight=1)
self.root.grid_columnconfigure(0, weight=1)

self.label_frame1.grid(row = 0,column = 0, sticky="ew")
self.list_box_frame.grid(row = 1,column = 0,sticky = "new")
self.music_box.grid(row = 1,column = 1,sticky = "nsew")

这需要照顾外框。 您还需要确保列表框展开以填充其空间:

self.list_box.grid(row = 0,column = 0,sticky = "nsew")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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