Tkinter yordamida bir toyifali o'lchovlar uchun konvertor oynasini tashkil etish.
Python GUI (grafik foydalanuvchi interfeysi) ni ishlab chiqish uchun bir nechta variantlarni taklif etadi. Barcha GUI usullaridan tkinter eng ko'p ishlatiladigan usul hisoblanadi. Bu Python bilan ta'minlangan Tk GUI asboblar to'plamiga standart Python interfeysi. Tkinterli Python GUI dasturlarini yaratishning eng tezkor va eng oson usulini chiqaradi. Tkinter yordamida GUI yaratish oson ish.
Tkinter yaratish uchun:
Modulni import qilish - tkinter
Asosiy oynani (konteyner) yarating
Asosiy oynaga istalgan miqdordagi vidjetni qo'shing
Vidjetlarda Trigger tadbirini qo'llang.
Python Tkinter moduli yordamida GUI asosidagi oddiy konvertor yarataylik, u asosiy arifmetik amallarni qo'shish, ayirish, ko'paytirish va bo'lishni bajarishi mumkin.
Quyida amalga oshirish:
from tkinter import *
window = Tk()
window.title("Converter")
window.geometry("500x300+500+350")
measurement1 = ""
measurement2 = ""
def convert_SI(val, unit_in, unit_out):
SI = {'Meter':1, 'Kilometer':1000, 'Centimeter':0.01, 'Millimeter':0.001,
'Micrometer':0.000001, 'Mile':1609.35, 'Yard':0.9144, 'Foot':0.3048,
'Inch':0.0254}
return val*SI[unit_in]/SI[unit_out]
def helpsection():
pass
def selectedInput():
global measurement1
measurement1 = listbox.get(listbox.curselection())
def selectedOutput():
global measurement2
measurement2 = listbox1.get(listbox1.curselection())
def converter():
try:
global measurement1, measurement2
result.set(str(convert_SI(float(inputEntry.get()), measurement1, measurement2)))
except:
result.set("Error")
title = Label(window, text="O'lchov birliklari Converteri", font="Calibri 16")
title.grid(columnspan=3)
result = StringVar()
filemenu = Menu(window)
filemenu.add_command(label='653-18', command=helpsection)
window.config(menu=filemenu)
inputEntry = Entry(window)
inputEntry.grid(row=1, column=0)
arrow = Label(window, text="--->", font="Calibri 20").grid(row=1, column=1)
outputEntry = Entry(window, textvariable=result).grid(row=1, column=2)
convertButton = Button(window, text='O\'tkazish', command=converter).grid(row=2, column=1)
scrollbar = Scrollbar(window)
scrollbar.grid(row=2, column=0, sticky = NE + SE)
listbox = Listbox(window, exportselection=False)
listbox.grid(row=2, column=0)
measurement_list = ['Meter', 'Kilometer', 'Centimeter', 'Millimeter',
'Micrometer', 'Mile', 'Yard', 'Foot', 'Inch']
for measurement in measurement_list:
listbox.insert(END, measurement)
listbox.bind("<>", lambda x: selectedInput())
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
scrollbar1 = Scrollbar(window)
scrollbar1.grid(row=2, column=2, sticky = NE + SE)
listbox1 = Listbox(window, exportselection=False)
listbox1.grid(row=2, column=2)
for measurement in measurement_list:
listbox1.insert(END, measurement)
listbox1.bind("<>", lambda x: selectedOutput())
listbox1.config(yscrollcommand=scrollbar1.set)
scrollbar1.config(command=listbox1.yview)
for i in range(3):
window.grid_rowconfigure(i, weight=1)
window.grid_columnconfigure(i, weight=1)
unit3 = input('')
Do'stlaringiz bilan baham: |