1
\$\begingroup\$

I started with Python and Tkinter and am trying to make my first steps by creating a simple UI with a header, sidebar (currently just a label) and a few labels and entries.

Before I go on I want to ask you all if the way I do this is correct and a common one. Also if you see potential to make this better, please let me know!

import tkinter as tk
from tkinter import messagebox
import tkinter.ttk as ttk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        root.geometry('1280x768')
        root.configure(background='#0E2F47')

        root.grid_rowconfigure(20, weight=1)        

        s = ttk.Style()
        s.theme_use('classic')        
        s.configure('SOExample.TEntry', relief='flat')
        s.layout('SOExample.TEntry', [
            ('Entry.highlight', {
                'sticky': 'nswe',
                'children':
                    [('Entry.border', {
                        'border': '1',
                        'sticky': 'nswe',
                        'children':
                            [('Entry.padding', {
                                'sticky': 'nswe',
                                'children':
                                    [('Entry.textarea',
                                      {'sticky': 'nswe'})]
                            })]
                    })]
            })])

        # GUI elements
        header_img = tk.PhotoImage(file='Header.png')

        header_label = tk.Label(root, bg='#191E31', image=header_img)
        header_label.image = header_img
        header_label.grid(column=1, row=0, columnspan=20)

        sidebar_label = tk.Label(root, bg='#191E31', width=25)
        sidebar_label.grid(column=1, row=1, rowspan=20, sticky="nws")

        # Input Section
        adress_input_label = tk.Label(root, bg='#0E273B', fg='#CEDEEE', text="Adress", font=("Century Gothic", 12), anchor='w')                                 
        adress_input_label.grid(column=2, columnspan=3, row=1, pady=25, sticky='new')

        adress_input_entry = ttk.Entry(root, style='SOExample.TEntry')
        adress_input_entry.grid(row=1, column=3, sticky='e')

        message_input_label = tk.Label(root, bg='#0E273B', fg='#CEDEEE', text="Message", font=("Century Gothic", 12), anchor='w')                                 
        message_input_label.grid(column=2, columnspan=3, row=2, pady=25, sticky='new')

        message_input_entry = ttk.Entry(root, style='SOExample.TEntry')
        message_input_entry.grid(row=2, column=3, sticky='e')


def on_closing():
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        root.destroy()

if __name__ == "__main__":
    root = tk.Tk()

    MainApplication(root).grid(column=0, row=0)

    root.protocol("WM_DELETE_WINDOW", on_closing)
    root.mainloop()
\$\endgroup\$
  • 2
    \$\begingroup\$ If your label and entry layout is incorrect, then unfortunately this is not a question for CodeReview, but rather StackOverflow. In my opinion, if you deem that behaviour "acceptable for now" and cut that part of the question out of your text, then the review request can remain. \$\endgroup\$ – Reinderien Jan 6 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.