Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

t.pack(side=BOTTOM, anchor='e')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading