Friday, March 15, 2019

Using Combobox in tkinter

Here is a simple Trigonometry App made with tkinter. This program will run only in Python 3. To run this program in Python 2, you will need to modify it accordingly. In this example you will learn how to use the Combobox control with tkinter.

Code:

import tkinter as tk
import tkinter.ttk as ttk
from tkinter import messagebox
import math
def onSelect(event=None):
if event:
value = event.widget.get()
label2['text'] = 'Length of ' + value + ':'
if value == 'Base':
label4['text'] = 'Perpendicular:'
label5['text'] = 'Hypotenuse:'
elif value == 'Perpendicular':
label4['text'] = 'Base:'
label5['text'] = 'Hypotenuse:'
elif value == 'Hypotenuse':
label4['text'] = 'Base:'
label5['text'] = 'Perpendicular:'
e1.set('')
e2.set('')
e3.set('')
e4.set('')
def calculate():
value = combo1.get()
try:
b = float(e1.get())
a = float(e2.get())
except:
messagebox.showwarning('Invalid Input','Please enter values for '+value+' and Angle.')
else:
if value == 'Base':
a = a*math.pi/180
p = b*math.tan(a)
h = b/math.cos(a)
e3.set(f'{p:0.2f}')
e4.set(f'{h:0.2f}')
elif value == 'Perpendicular':
p = b
a = a*math.pi/180
b = p/math.tan(a)
h = p/math.sin(a)
e3.set(f'{b:0.2f}')
e4.set(f'{h:0.2f}')
elif value == 'Hypotenuse':
h = b
a = a*math.pi/180
b = h*math.cos(a)
p = h*math.sin(a)
e3.set(f'{b:0.2f}')
e4.set(f'{p:0.2f}')
root = tk.Tk()
root.title('Trigonometry')
e1 = tk.StringVar()
e2 = tk.StringVar()
e3 = tk.StringVar()
e4 = tk.StringVar()
label1 = tk.Label(root,text='Select a side of right triangle:')
combo1 = ttk.Combobox(root, values=('Base','Perpendicular','Hypotenuse'),state='readonly')
combo1.set('Base')
combo1.bind('<<ComboboxSelected>>', onSelect)
label2 = tk.Label(root,text='Length of Base:')
entry1 = tk.Entry(root,textvariable=e1)
label3 = tk.Label(root,text='Angle (in Degrees):')
entry2 = tk.Entry(root,textvariable=e2)
btn1 = tk.Button(root,text='Calculate',command=calculate)
label4 = tk.Label(root,text='Perpendicular:')
entry3 = tk.Entry(root,textvariable=e3,state='readonly')
label5 = tk.Label(root,text='Hypotenuse:')
entry4 = tk.Entry(root,textvariable=e4,state='readonly')
label1.grid(row=0,column=0,padx=(10,5),pady=(10,5),sticky='e')
combo1.grid(row=0,column=1,padx=(5,10),pady=(10,5),sticky='w')
label2.grid(row=1,column=0,padx=(10,5),pady=(5,5),sticky='e')
entry1.grid(row=1,column=1,padx=(5,5),pady=(5,5),sticky='w')
label3.grid(row=2,column=0,padx=(10,5),pady=(5,5),sticky='e')
entry2.grid(row=2,column=1,padx=(5,5),pady=(5,5),sticky='w')
btn1.grid(row=3,column=1,padx=(5,5),pady=(5,5),sticky='w')
label4.grid(row=4,column=0,padx=(10,5),pady=(5,5),sticky='e')
entry3.grid(row=4,column=1,padx=(5,5),pady=(5,5),sticky='w')
label5.grid(row=5,column=0,padx=(10,5),pady=(5,10),sticky='e')
entry4.grid(row=5,column=1,padx=(5,5),pady=(5,10),sticky='w')
root.resizable(False,False)
root.mainloop()
view raw Trigonometry.py hosted with ❤ by GitHub

Output:


To know more about TkInter, please visit tkinter documentation page here.

No comments: