Python Forum
Created a simple GUI to get myself started with TkInter!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Created a simple GUI to get myself started with TkInter!
#1
[Image: DiscountApp.jpg]
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()
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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.
Reply
#4
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.
Reply
#5
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Current project - Simple tkinter login system menator01 3 1,541 Sep-01-2023, 05:56 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020