Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python: Age Calculator using Tkinter
Next article icon

Prerequisites:Python GUI – tkinter
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is 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.
 

Steps to Create a Tkinter: 
 

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

Below is what the GUI looks like:


Let's create a GUI based weight converter that accepts a kilogram input value and converts that value to grams, pounds, and ounces when the user clicks the Convert button.


Below is the implementation.
 

Python3
# Python program to  create a simple GUI# weight converter using Tkinterfromtkinterimport*# Create a GUI windowwindow=Tk()# Function to convert weight# given in kg to grams, pounds# and ouncesdeffrom_kg():# convert kg to gramgram=float(e2_value.get())*1000# convert kg to poundpound=float(e2_value.get())*2.20462# convert kg to ounceounce=float(e2_value.get())*35.274# Enters the converted weight to# the text widgett1.delete("1.0",END)t1.insert(END,gram)t2.delete("1.0",END)t2.insert(END,pound)t3.delete("1.0",END)t3.insert(END,ounce)# Create the Label widgetse1=Label(window,text="Enter the weight in Kg")e2_value=StringVar()e2=Entry(window,textvariable=e2_value)e3=Label(window,text='Gram')e4=Label(window,text='Pounds')e5=Label(window,text='Ounce')# Create the Text Widgetst1=Text(window,height=1,width=20)t2=Text(window,height=1,width=20)t3=Text(window,height=1,width=20)# Create the Button Widgetb1=Button(window,text="Convert",command=from_kg)# grid method is used for placing# the widgets at respective positions# in table like structuree1.grid(row=0,column=0)e2.grid(row=0,column=1)e3.grid(row=1,column=0)e4.grid(row=1,column=1)e5.grid(row=1,column=2)t1.grid(row=2,column=0)t2.grid(row=2,column=1)t3.grid(row=2,column=2)b1.grid(row=0,column=2)# Start the GUIwindow.mainloop()

Output:


Weight Conversion GUI using Tkinter
Improve

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