How to set a tkinter textbox to be in the button right of the window

Question:

I am working on a simple UI for my electronics project application, I am using tkinter as my window and want to add a text box that will be used as a logger. My screen size is the window’s initial size and I like the textbox to be at the bottom right of the window.

this is a test code that I am using to try and figure it out:

class Window:
    def __init__(self, WIDTH, HEIGHT, WIN) -> None:
        """
        Parameters
        ----------
        self.width : int
            The width of )the window created.
        self.height : int
            The height of the window created.
        self.window : tk.TK
            The window object.

        Variables
        ---------
        self.data : dict
            A dictonary containing all information about buttons creates and titles displayed on screen.
        """
        self.width  = WIDTH
        self.height = HEIGHT
        self.window = WIN

        self.data : dict = {
            "Title" : None,
            "btns" : {}
        }

    def constract(self, title : str, background_color : str) -> None:
        """
        Creates the main window adds a title and a background color.

        Parameters
        ----------
        title : str
            A string which will serve as the window's title.
        backgound_color : str
            A string represents a hex code color (e.g. #FFFFFF).
        """
        self.window.title(title)
        self.window.geometry(f'{self.width}x{self.height}')
        self.window.configure(bg=background_color)

    def header(self, text : str, fill : str = "black", font : str = 'Arial 28 bold', background : str ='#E1DCDC') -> None:
        """
        Displays a title on the screen.

        Parametes
        ---------
        text : str
        The text which will be displayed on the screen.
        fill : str
            The color of the text, can be an existing color in tkinter or a custom hex code color (e.g. #FFFFFF).
        font : str
            The font type, the size of the letters and other designs for the text.
        backgound : str
            The color of the box around the text, can be an existing color in tkinter or a custom hex code color (e.g. #FFFFFF).
        """
        T = Label(self.window, text=text, bg=background ,fg=fill, font=font)
        T.pack()
        self.data["Title"] = text

class PrintLogger(): # create file like object
    def __init__(self, textbox): # pass reference to text widget
        self.textbox = textbox # keep ref

    def write(self, text):
        self.textbox.insert(tk.END, text) # write text to textbox
            # could also scroll to end of textbox here to make sure always visible

    def flush(self): # needed for file like object
        pass

if __name__ == '__main__':
    from tkinter import *
    import sys

    win = Tk()

    WIDTH, HEIGHT = win.winfo_screenwidth(), win.winfo_screenheight()
    main_window = Window(WIDTH, HEIGHT, win)

    main_window.constract("AntiBalloons system controls", "#E1DCDC")
    main_window.header("AntiAir system controls")

    t = Text(win, bg="#E1DCDC",bd=1)
    t.tag_configure("", justify='right')
    t.pack()

    pl = PrintLogger(t)

    sys.stdout = pl

    main_window.window.mainloop()
Asked By: David

||

Answers:

Try this:

t.pack(side=BOTTOM, anchor='e')
Answered By: toyota Supra
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.