Tkinter文本小部件会使列大小变形

时间:2019-05-02 20:42:23

标签: python python-3.x tkinter widget

我无法像图片中那样将小部件打包在行或列中,您能帮我吗? enter image description here

问题是文本小部件使​​列大小变形,文本不应该在row = 3,column = 0中吗? enter image description here

   def _formato(self):
    t1=tkinter.Toplevel(self._finestra)
    labelTop = tkinter.Label(t1,text = "Tipo di carattere")
    labelTop.grid(row=0, column=0)
    labelTop2 = tkinter.Label(t1,text = "Dimensione")
    labelTop2.grid(row=0, column=1)
    labelTop3 = tkinter.Label(t1)
    labelTop3.grid(row=2, column=0)
    listaFont=tkinter.ttk.Combobox(t1)
    allfonts = sorted(tkinter.font.families())
    listaFont["values"] =  allfonts
    listaFont.grid(row=1, column=0)
    listaFont.bind("<<ComboboxSelected>>", None)
    listaDimensione = tkinter.ttk.Combobox(t1)
    allfontsizes = list(range(8,70))
    listaDimensione['values'] =  allfontsizes
    listaDimensione.grid(row=1, column=1)
    testo= tkinter.Text(t1)
    testo.insert(tkinter.INSERT,'AaBbYyZz')
    testo.grid(row=3,column=0)

1 个答案:

答案 0 :(得分:0)

  

问题:同一column中的所有小部件都应具有相等的width

核心是为每个列使用Frame,并将小部件布置到Frame中。
这样一来,所有小部件都可以调整为Frame width的大小。
enter image description here


  1. 为演示目的定义class App

    class App(tk.Tk):
        def __init__(self):
            super().__init__()
    
  2. 要获得相等的width,请为每列使用tk.Frame
    允许Frame增长width
    允许Frame内的小部件增加其width
    定义Frame以成长为App width

            # column 0
            self.grid_columnconfigure(0, weight=1)
            frame_0 = tk.Frame(self)
            frame_0.grid_columnconfigure(0, weight=1)
            frame_0.grid(row=0, column=0, sticky='nsew')
    
  3. 添加小部件...
    定义每个小部件,以将其width增长到Frame width

            labelTop = tkinter.Label(frame_0, text="Tipo di carattere")
            labelTop.grid(row=0, column=0, sticky='ew')
    
            listaFont = tkinter.ttk.Combobox(frame_0)
            listaFont.grid(row=1, column=0, sticky='ew')
            allfonts = sorted(tkinter.font.families())
            listaFont["values"] = allfonts
            listaFont.bind("<<ComboboxSelected>>", None)
    
  4. 注意:使用width=1

    重置默认值
            testo = tkinter.Text(frame_0, width=1)
            testo.insert(tkinter.INSERT, 'AaBbYyZz')
            testo.grid(row=3, column=0, sticky='ew')
    
  5. 第1列相同...

            # column 1
            self.grid_columnconfigure(1, weight=1)
            frame_1 = tk.Frame(self)
            frame_1.grid_columnconfigure(0, weight=1)
            frame_1.grid(row=0, column=1, sticky='nsew')
    
            labelTop2 = tkinter.Label(frame_1, text="Dimensione")
            labelTop2.grid(row=0, column=0, sticky='ew')
    
            listaDimensione = tkinter.ttk.Combobox(frame_1)
            allfontsizes = list(range(8, 70))
            listaDimensione['values'] = allfontsizes
            listaDimensione.grid(row=1, column=0, sticky='ew')
    
  

用法

    if __name__ == "__main__":
        App().mainloop()

使用Python测试:3.5