Search code examples
pythontkinterwidgetgrid

How to sticky widget inside frame?


I tried to sticky Entry widgets to right (by sticky='e') and labels to left, but entry widgets are "sticky" to left.

enter image description here

Could you help me? Maybe i should do extrack the widgets out of frames to single main frame?

My code:

class LabeledEntry(tk.Frame):

    def __init__(self,
                 master: Union[tk.Misc, None] = None,
                 label_text='', default: str = '') -> None:
        super().__init__(master,
                         width=250,
                         height=30,
                         border=3,
                         borderwidth=2,
                         highlightthickness=2,
                         highlightbackground='blue')
        self.grid_propagate(False)
        self._label = tk.Label(self,
                               text=label_text)
        self._label.grid(row=0,
                         column=0)
        self._entry = tk.Entry(self,
                               width=17)
        self._entry.grid(row=0,
                         column=1,
                         sticky='e')
        if default:
            self._entry.insert(0, default)
class ConfigurationFrame(tk.Frame):
    def __init__(self, master) -> None:
        super().__init__(master)

        # TODO rozciagnac na cala szerokosc
        self.dchg_current = LabeledEntry(self, "x")
        self.dchg_current.grid(row=0, column=0, sticky="NESW", columnspan=2)
        self.cutoff_volt = LabeledEntry(self, "C                  X")
        self.cutoff_volt.grid(row=1, column=0, sticky="NESW", columnspan=2)

Solution

  • If you want to put the entry box to the right, you need to make column 1 of the frame to occupy all the available horizontal space using self.columnconfigure(1, weight=1):

    class LabeledEntry(tk.Frame):
    
        def __init__(self,
                     master: Union[tk.Misc, None] = None,
                     label_text='', default: str = '') -> None:
            super().__init__(master,
                             width=250,
                             height=30,
                             border=3,
                             borderwidth=2,
                             highlightthickness=2,
                             highlightbackground='blue')
            self.grid_propagate(False)
            self.columnconfigure(1, weight=1)
            ...
    

    Result:

    enter image description here


    Note that it is easier when using pack() instead of grid():

    class LabeledEntry(tk.Frame):
        def __init__(self,
                     master: Union[tk.Misc, None] = None,
                     label_text='', default: str = '') -> None:
            super().__init__(master,
                             width=250,
                             height=30,
                             border=3,
                             borderwidth=2,
                             highlightthickness=2,
                             highlightbackground='blue')
            self.pack_propagate(False)
            self._label = tk.Label(self, text=label_text)
            self._label.pack(side="left")
    
            self._entry = tk.Entry(self, width=17)
            self._entry.pack(side="right")
    
            if default:
                self._entry.insert(0, default)