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


Messages In This Thread
Created a simple GUI to get myself started with TkInter! - by bixitz - Apr-04-2019, 01:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Current project - Simple tkinter login system menator01 3 1,697 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