Этот вопрос закрыт для ответов, так как повторяет вопрос Как добавить элементы в Combobox в засимости от выбранного Radiobox'а (Python)?
@marlinevych

Как сделать конвертер единиц в tkinter?

Мне нужно сделать конвернер единиц в Python используя модуль tkinter, но у меня есть проблемы из событиями и рассчетами, которые должны происходить

Я никак не могу сделать так, чтобы при выборе одного из переключателей Radiobutton в списке Combobox'а обновлялись соответствующие элементы (это 1 шаг, с которым я сейчаас хочу разобратся).

Код на данный момент:

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()
  • Вопрос задан
  • 551 просмотр
Решения вопроса 1
Vindicar
@Vindicar
RTFM!
Для value_type пропиши callback на изменение значения.
self.value_type = IntVar()
self.value_type.trace_add('write', self.select_value_type)

При этом метод select_value_type должен иметь сигнатуру вида
def select_value_type(self, *args):
В твоём случае параметры метода не важны, так как он связан с изменением одной переменной. Вот почему можно просто прописать *args.
Ответ написан
Ваш ответ на вопрос

Вопрос закрыт для ответов и комментариев

Потому что уже есть похожий вопрос.
Похожие вопросы