2

I can't pack widgets in rows or columns as in image, can you help me?

enter image description here

The problem is text widget deforms column size, text should not be in 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)
Barbora
  • 921
  • 1
  • 6
  • 11
antonio
  • 59
  • 6
  • Read [gui layout using frames and grid](https://stackoverflow.com/questions/34276663/tkinter-gui-layout-using-frames-and-grid/34277295#34277295) – stovfl May 02 '19 at 21:33
  • Can you please add some more details what you are trying to achieve, what is working and what isn't? – Jere May 02 '19 at 21:34
  • @Jere: The OP want to layout widgets in a `grid` manner, but uses `pack` without options. – stovfl May 03 '19 at 06:37
  • @stovfl I was not clear yesterday. the problem is text widget deforms column size. I could change the width of it but it is not accurate... – antonio May 03 '19 at 12:12
  • @stovfl No sorry, only in row=3 ,column=0 and row=4, column=0.But it goes well beyond the column 0 – antonio May 03 '19 at 13:29
  • @antonio You want the `Text` to be equal `width` as the above `Label(t1, text="Tipo di carattere")`? – stovfl May 03 '19 at 14:07
  • 1
    @stovfl Yes. At the moment I solved by modifying the width of the Text – antonio May 03 '19 at 14:25

1 Answers1

1

Question: All widgets in the same column should have equal width.

The core is, use a Frame for every column and layout the widgets into the Frame.
This allows all widgets to resize to the Frame width.
enter image description here


  1. Define class App for demonstration purpose

    class App(tk.Tk):
        def __init__(self):
            super().__init__()
    
  2. To get equal width use a tk.Frame for every column.
    Allow the Frame to grow his width.
    Allow the widgets inside the Frame to grow his width.
    Define the Frame to grow up to the 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. Add the widgets ...
    Define every widget to grow his width up to the 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. Note: Reset the default using width=1

            testo = tkinter.Text(frame_0, width=1)
            testo.insert(tkinter.INSERT, 'AaBbYyZz')
            testo.grid(row=3, column=0, sticky='ew')
    
  5. The same for columen 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')
    

Usage:

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

Tested with Python: 3.5

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • only question, why using width=1 on Text? testo = tkinter.Text(frame_0, width=1) – antonio May 03 '19 at 16:51
  • @antonio: `Text` widget is greedy about `width/height`, it instantiats with a hugh `width`. It's a workaround to force the `width` to obey the `Frame width`. – stovfl May 03 '19 at 16:54
  • @antonio *" place frame inside a toplevel?"*: It's the same, change to `class App(tk.Toplevel):` and in `__main__` do at first `root = tk.Tk()`. The `root` is used by default. – stovfl May 03 '19 at 18:08
  • Only a problem,I set the height of Text widget to 3, when I increase the font size it grows in height... – antonio May 04 '19 at 18:15
  • @antonio: Read about [how-to-stop-tkinter-text-widget-resize-on-font-change](https://stackoverflow.com/questions/9833698/how-to-stop-tkinter-text-widget-resize-on-font-change/9839014#9839014) – stovfl May 04 '19 at 18:25