3
\$\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 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()
toolic
14.4k5 gold badges29 silver badges201 bronze badges
asked Jan 6, 2019 at 12:51
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

It looks like you have the good beginnings for a basic GUI application.

Naming

The code uses the recommended naming style for classes, functions and variables. Most of the names are descriptive. However, s isn't very meaningful in this context:

s = ttk.Style()

I suggest style instead:

style = ttk.Style()

I realize this is currently a generic GUI, but once you create something more specific, you should rename MainApplication to reflect the actual use.

Documentation

The PEP 8 style guide recommends adding docstrings for classes and functions. Again, once you have a specific use, you should add a docstring to describe the class.

answered Mar 14 at 18:19
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.