Tkinter 自定义小部件列未填充框架的整个宽度(在主窗口内)

问题描述 投票:0回答:1

数据库中的参数将被迭代到自定义小部件中,该小部件将被打包到预定义的框架中(具有特定的宽度、高度和位置)。自定义小部件功能如下所示:

class customWidget(Frame):
    def __init__(self, master, firstName, lastName, phoneNumber, email):
      Frame.__init__(self, master, bd=2, relief='raised', width=911, bg="#F8F9FB", borderwidth=0,highlightthickness=0)

      first_name_label = Label(self, text=firstName)
      last_name_label = Label(self, text=lastName)
      phone_number_label = Label(self, text=phoneNumber)
      email_label = Label(self, text=email)

      self.columnconfigure(0, weight=1)
      self.columnconfigure(1, weight=1)
      self.columnconfigure(2, weight=1)
      self.columnconfigure(3, weight=1)
      
      first_name_label.grid(row=0, column=0, padx=5, pady=5)
      last_name_label.grid(row=0, column=1, padx=5, pady=5)
      phone_number_label.grid(row=0, column=2, padx=5, pady=5)
      email_label.grid(row=0, column=3, padx=5, pady=5, sticky="e")

      self.pack(expand=False, fill="x", side="top", pady=(5,10))

示例小部件:

PersonsFrame = Frame(window, width=911, height=476, bg="#f8f9fb")
PersonsFrame.pack()
PersonsFrame.place(x=228,y=175)

customWidget(PersonsFrame, "John", "Cena", "+99 99 9999 999", "[email protected]")
customWidget(PersonsFrame, "Barack", "Obama", "+99 99 9999 999", "[email protected]")
customWidget(PersonsFrame, "Borat", "Sagdiyev", "+99 99 9999 999", "[email protected]")

尽管已经设置了列权重,但结果却出乎意料。托管自定义小部件的人员框架的宽度为 911,但大部分宽度都没有被利用,只是尽可能最小化。

Gone wrong 由于未使用全角,因此数据库中的数据未对齐。问题/解决方案可能是什么?

我尝试配置列和权重,但没有成功。

 self.columnconfigure(0, weight=1)
 self.columnconfigure(1, weight=1)
 self.columnconfigure(2, weight=1)
 self.columnconfigure(3, weight=1)
python user-interface tkinter grid
1个回答
0
投票

感谢德里克。解决方案是使用网格管理器并将列网格的 unifrom 配置设置为 True。 Sticky = 每个字段的nsew。省略 self.pack 因为我们使用的是网格管理器。

  self.grid_columnconfigure(0, weight=1, uniform="True")
  self.grid_columnconfigure(1, weight=1, uniform="True")

  self.grid()
 
  first_name.grid(row=0, column=0, padx=5, pady=5, sticky="nsew")
  last_name.grid(row=0, column=1, padx=5, pady=5, sticky="nsew")

  #Remove self.pack(expand=False, fill="x", side="top", pady=(5,10))
© www.soinside.com 2019 - 2024. All rights reserved.