Answer the question
In order to leave comments, you need to log in
How to make unit converter in tkinter?
I need to make a unit converter in Python using the tkinter module, but I'm having problems with the events and calculations that need to happen
. I can't seem to make it so that when one of the Radiobuttons in the Combobox's list is selected, the corresponding items are updated (this is 1 step , which I now want to deal with).
Code at the moment:
import tkinter as tk
import tkinter.ttk as ttk
length=["км", "дм", "см", "мм"]
speed=["км/ч", "км/мин", "м/ч", "м/мин"]
class ConverteruiApp:
def __init__(self, master=None):
# build ui
self.mainwindow = ttk.Frame(master)
self.title = tk.Label(self.mainwindow)
self.title.configure(anchor='w', font='{gilroy} 16 {bold}', relief='flat', text='Конвертер ')
self.title.place(anchor='nw', relx='0.20', rely='0.03', x='0', y='0')
value_type=tk.IntVar()
self.radiobutton1 = tk.Radiobutton(self.mainwindow)
self.radiobutton1.configure(value=1, variable=value_type, anchor='w', font='{gilroy medium} 12 {}', text='Длина')
self.radiobutton1.place(anchor='nw', relx='0.12', rely='0.13', x='0', y='0')
self.radiobutton2 = tk.Radiobutton(self.mainwindow)
self.radiobutton2.configure(value=2, variable=value_type, font='{gilroy medium} 12 {}', text='Время')
self.radiobutton2.place(anchor='nw', relx='0.41', rely='0.13', x='0', y='0')
self.radiobutton3 = tk.Radiobutton(self.mainwindow)
self.radiobutton3.configure(value=3, variable=value_type, anchor='w', font='{gilroy medium} 12 {}', text='Скорость')
self.radiobutton3.place(anchor='n', relx='0.75', rely='0.13', x='0', y='0')
self.label1 = tk.Label(self.mainwindow)
self.label1.configure(compound='top', cursor='arrow', font='{gilroy} 12 {bold}', text='Перевести:')
self.label1.place(anchor='nw', relx='0.13', rely='0.25', x='0', y='0')
self.entry1 = tk.Entry(self.mainwindow)
self.entry1.configure(cursor='arrow', exportselection='true', font='{gilroy medium} 16 {}')
self.entry1.place(anchor='nw', relx='0.13', rely='0.32', x='0', y='0')
self.combobox1 = ttk.Combobox(self.mainwindow)
self.combobox1.configure(cursor='hand2', width='45')
self.combobox1.place(anchor='nw', relheight='0.05', relwidth='0.75', relx='0.13', rely='0.40', x='0', y='0')
self.combobox2 = ttk.Combobox(self.mainwindow)
self.combobox2.configure(cursor='hand2', width='45')
self.combobox2.place(anchor='nw', relheight='0.05', relwidth='0.75', relx='0.13', rely='0.55', x='0', y='0')
self.label2 = tk.Label(self.mainwindow)
self.label2.configure(font='{gilroy} 12 {bold}', text='В:')
self.label2.place(anchor='nw', relx='0.13', rely='0.48', x='0', y='0')
self.button1 = tk.Button(self.mainwindow)
self.button1.configure(anchor='n', cursor='hand2', font='{gilroy} 14 {bold}', text='Перевести')
self.button1.place(anchor='nw', relx='0.32', rely='0.64', x='0', y='0')
self.entry2 = tk.Entry(self.mainwindow)
self.entry2.configure(font='{gilroy light} 16 {bold}', state='readonly')
_text_ = '''
'''
self.entry2['state'] = 'normal'
self.entry2.delete('0', 'end')
self.entry2.insert('0', _text_)
self.entry2['state'] = 'readonly'
self.entry2.place(anchor='nw', relx='0.13', rely='0.81', x='0', y='0')
self.label4 = tk.Label(self.mainwindow)
self.label4.configure(font='{gilroy} 12 {bold}', text='Результат:')
self.label4.place(anchor='nw', relx='0.13', rely='0.74', x='0', y='0')
self.mainwindow.configure(height='540', padding='0', width='380')
self.mainwindow.pack(side='top')
# Main widget
self.mainwindow = self.mainwindow
def select_value_type():
if value_type.get()==1:
self.combobox1.['values']=length
elif value_type.get()==2:
self.combobox1['values']=speed
def run(self):
self.mainwindow.mainloop()
if __name__ == '__main__':
import tkinter as tk
root = tk.Tk()
app = ConverteruiApp(root)
app.run()
Answer the question
In order to leave comments, you need to log in
For value_type write a callback to change the value.
self.value_type = IntVar()
self.value_type.trace_add('write', self.select_value_type)
def select_value_type(self, *args):
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question