Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Compound Interest GUI Calculator using Tkinter
Next article icon

Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods,Tkinteris the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.

To create a Tkinter:

  1. Importing the module – tkinter
  2. Create the main window (container)
  3. Add any number of widgets to the main window
  4. Apply the event Trigger on the widgets.

Below is what the GUI looks like:

GUI
GUI

Let's create a GUI-based simple calculator using the Python Tkinter module, which can perform basic arithmetic operations addition, subtraction, multiplication, and division.

Below is the implementation: 

Python
fromtkinterimport*expr=""# Global expression stringdefpress(key):globalexprexpr+=str(key)display.set(expr)defequal():globalexprtry:result=str(eval(expr))display.set(result)expr=""except:display.set("error")expr=""defclear():globalexprexpr=""display.set("")if__name__=="__main__":root=Tk()root.configure(bg="light green")root.title("Simple Calculator")root.geometry("270x150")display=StringVar()entry=Entry(root,textvariable=display)entry.grid(columnspan=4,ipadx=70)# Number buttonsbtn1=Button(root,text='1',fg='black',bg='red',command=lambda:press(1),height=1,width=7)btn1.grid(row=2,column=0)btn2=Button(root,text='2',fg='black',bg='red',command=lambda:press(2),height=1,width=7)btn2.grid(row=2,column=1)btn3=Button(root,text='3',fg='black',bg='red',command=lambda:press(3),height=1,width=7)btn3.grid(row=2,column=2)btn4=Button(root,text='4',fg='black',bg='red',command=lambda:press(4),height=1,width=7)btn4.grid(row=3,column=0)btn5=Button(root,text='5',fg='black',bg='red',command=lambda:press(5),height=1,width=7)btn5.grid(row=3,column=1)btn6=Button(root,text='6',fg='black',bg='red',command=lambda:press(6),height=1,width=7)btn6.grid(row=3,column=2)btn7=Button(root,text='7',fg='black',bg='red',command=lambda:press(7),height=1,width=7)btn7.grid(row=4,column=0)btn8=Button(root,text='8',fg='black',bg='red',command=lambda:press(8),height=1,width=7)btn8.grid(row=4,column=1)btn9=Button(root,text='9',fg='black',bg='red',command=lambda:press(9),height=1,width=7)btn9.grid(row=4,column=2)btn0=Button(root,text='0',fg='black',bg='red',command=lambda:press(0),height=1,width=7)btn0.grid(row=5,column=0)# Operator buttonsplus=Button(root,text='+',fg='black',bg='red',command=lambda:press('+'),height=1,width=7)plus.grid(row=2,column=3)minus=Button(root,text='-',fg='black',bg='red',command=lambda:press('-'),height=1,width=7)minus.grid(row=3,column=3)mult=Button(root,text='*',fg='black',bg='red',command=lambda:press('*'),height=1,width=7)mult.grid(row=4,column=3)div=Button(root,text='/',fg='black',bg='red',command=lambda:press('/'),height=1,width=7)div.grid(row=5,column=3)# Other buttonseq=Button(root,text='=',fg='black',bg='red',command=equal,height=1,width=7)eq.grid(row=5,column=2)clr=Button(root,text='Clear',fg='black',bg='red',command=clear,height=1,width=7)clr.grid(row=5,column=1)dot=Button(root,text='.',fg='black',bg='red',command=lambda:press('.'),height=1,width=7)dot.grid(row=6,column=0)root.mainloop()

Output

Explanation:

  • press(key)appends input to expr and updates the display,equal() evaluates expr and shows result or error,clear() resets everything.
  • Sets up the main window with title, size, background and aStringVardisplay linked to an entry widget for showing input/output.
  • Numeric buttons (0–9) arranged in a grid, each callspress()with its number.
  • Operator buttons (+, -, *, /) placed in the grid, each callspress() with the operator symbol.
  • Additional buttons: = triggers calculation, Clear resets and. adds a decimal point.
  • Uses grid() layout for neat positioning buttons have uniform size and color, runs the Tkinter main event loop.

Simple GUI calculator using Tkinter
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp