Created a simple GUI to get myself started with TkInter! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Created a simple GUI to get myself started with TkInter! (/thread-17270.html) |
Created a simple GUI to get myself started with TkInter! - bixitz - Apr-04-2019 Hello friends, I have created this simple GUI using TkInter. Am I doing it right? Here is the link to my code import tkinter as tk from tkinter import messagebox class myApp: def __init__(self): self.root = tk.Tk() self.e1 = tk.StringVar() self.e2 = tk.StringVar() self.e3 = tk.StringVar() label1 = tk.Label(self.root, text='Price:') label2 = tk.Label(self.root, text='Discount:') label3 = tk.Label(self.root, text='%') label4 = tk.Label(self.root, text='Amount:') entry1 = tk.Entry(self.root, textvariable=self.e1) entry2 = tk.Entry(self.root, textvariable=self.e2) entry3 = tk.Entry(self.root, textvariable=self.e3, state='readonly') btn1 = tk.Button(self.root, text='Calculate',command=button_click) label1.grid(row=0,column=0,padx=(10,5),pady=(10,5),sticky=tk.W) entry1.grid(row=0,column=1,padx=(5,5),pady=(10,5)) label2.grid(row=1,column=0,padx=(10,5),pady=(5,5),sticky=tk.W) entry2.grid(row=1,column=1,padx=(5,5),pady=(5,5)) label3.grid(row=1,column=2,padx=(5,10),pady=(5,5)) btn1.grid(row=2,column=1,padx=(5,5),pady=(5,5),sticky=tk.W) label4.grid(row=3,column=0,padx=(10,5),pady=(5,10),sticky=tk.W) entry3.grid(row=3,column=1,padx=(5,5),pady=(5,10),sticky=tk.W) self.root.resizable(False,False) self.root.title('Discount App') def button_click(): try: price = float(app.e1.get()) discount = float(app.e2.get()) except: messagebox.showwarning('Invalid Input','Please enter numbers only.') else: amount = price*(1-discount/100) app.e3.set(str(f'{amount:0.2f}')) app = myApp() app.root.mainloop() RE: Created a simple GUI to get myself started with TkInter! - buran - Apr-04-2019 Please, post your code in python tags here. If you want to promote your blog - let us know and it may be included in the designated thread after review. OK, I will post your code for you this time RE: Created a simple GUI to get myself started with TkInter! - nilamo - Apr-04-2019 (Apr-04-2019, 01:22 PM)bixitz Wrote: Am I doing it right?If that's a request for suggestions, then... 1) You don't follow PEP8 for naming or formatting. For code you intend to share, following it is very helpful: https://www.python.org/dev/peps/pep-0008/ If you check pip, there's a program called pylint which will read your file, and let you know how you can improve it.Here's the first few lines when run against your program: >pylint spam.py ************* Module spam spam.py:3:0: C0303: Trailing whitespace (trailing-whitespace) spam.py:17:52: C0326: Exactly one space required after comma btn1 = tk.Button(self.root, text='Calculate',command=button_click) ^ (bad-whitespace) spam.py:18:0: C0303: Trailing whitespace (trailing-whitespace) spam.py:19:25: C0326: Exactly one space required after comma label1.grid(row=0,column=0,padx=(10,5),pady=(10,5),sticky=tk.W) ^ (bad-whitespace)2) On line 38, you have this: app.e3.set(str(f'{amount:0.2f}')) . There's no reason to call str() on a string. It's already a string.
RE: Created a simple GUI to get myself started with TkInter! - bixitz - Apr-04-2019 Teaching programming for 2 decades doesn't mean I cannot learn Python as a new language. No, I don't want to promote my blog. I just wanted to share only to those who are interested to see. That's why I had included a picture of the app, so that people who are not interested need not waste their time. Anyways, Thanks. RE: Created a simple GUI to get myself started with TkInter! - buran - Apr-04-2019 So, everything is fine - your post is in the right sub-forum, the code is here to be seen and you will get bunch of good advise |