0

i'm making a unit converter in python, it has 3 options menus, the option menu one it contains the type of the thing that you are trying to convert (pression, temperatury, time, data transmission, velocity, area, etc), the option menu two and three is according the option menu one, if you put in the option menu one that you want to convert some value of velocity the menus two and three will only show velocity values.

When i run the code and select any option from menus two and three it prints the value that i selected from both, but when i select any option from menu one it doesn't print what i did select anymore.

I have no idea of how to fix it.

Code bellow:

Code from GUI:

from tkinter import *
import tkinter as tk
from itertools import product
from Converter import *

converter = Converter()

window = Tk()

w = 650
h = 300

w_screen = window.winfo_screenwidth()
h_screen = window.winfo_screenheight()

position_x = (w_screen/2) - (w/2)
position_y = (h_screen/2) - (h/2)

window.geometry(f'{w}x{h}+{int(position_x)}+{int(position_y)}')
window.configure(
    background='gray'
)
window.resizable(False, False)


def change_options_menu(event):
    converter.change_options_menu(
        stringVar_drop_down_1, stringVar_drop_down_2, stringVar_drop_down_3, drop_down_2, drop_down_3)


options_drop_down_1 = [
    'Armazenamento de Dados',
    'Comprimento',
    'Consumo de Combustível',
    'Energia Mecânica',
    'Frequência',
    'Massa',
    'Pressão',
    'Temperatura',
    'Tempo',
    'Transmissão de dados',
    'Velocidade',
    'Volume',
    'Área',
    'Ângulo'
]
default_drop_down_1 = options_drop_down_1[1]
stringVar_drop_down_1 = StringVar(window)
stringVar_drop_down_1.set(default_drop_down_1)
drop_down_1 = OptionMenu(
    window,
    stringVar_drop_down_1,
    *options_drop_down_1,
    command=change_options_menu
)

drop_down_1['width'] = w
drop_down_1['font'] = 'Arial 20'
drop_down_1['anchor'] = 'w'

options_drop_down_2 = converter.options_comprimento
default_drop_down_2 = options_drop_down_2[1]
stringVar_drop_down_2 = StringVar(window)
stringVar_drop_down_2.set(default_drop_down_2)
drop_down_2 = OptionMenu(
    window,
    stringVar_drop_down_2,
    *options_drop_down_2
)

drop_down_2['width'] = 15
drop_down_2['font'] = 'Arial 20'
drop_down_2['anchor'] = 'w'

options_drop_down_3 = converter.options_comprimento
default_drop_down_3 = options_drop_down_3[1]
stringVar_drop_down_3 = StringVar(window)
stringVar_drop_down_3.set(default_drop_down_3)
drop_down_3 = OptionMenu(
    window,
    stringVar_drop_down_3,
    *options_drop_down_3
)

drop_down_3['width'] = 15
drop_down_3['font'] = 'Arial 20'
drop_down_3['anchor'] = 'w'


entry = Entry(
    window,
    width=19,
    font='Arial 19'
)

label_result = Label(
    window,
    font='Arial 18',
    width=19
)

label_equal = Label(
    window,
    text='=',
    font='Arial 20',
    width=5,
    bg='gray'
)

label_formula = Label(
    window,
    font='Arial 20',
    bg='gray',
    text='Fórmula: '
)

drop_down_1.pack()
drop_down_2.place(x=7, y=150)
drop_down_3.place(x=375, y=150)
entry.place(x=7, y=110)
label_result.place(x=375, y=110)
label_equal.place(x=283, y=153)
label_formula.place(x=7, y=240)

window.mainloop()

Code from the classe converter, that it's the backend of my application:

import tkinter as tk


class Converter:
    def __init__(self):
        self.options_armazenamento_de_dados = [
            'Bit',
            'Kilobit',
            'Kibibit',
            'Megabit',
            'Megbibit',
            'Gigabit',
            'Gibibit',
            'Terabit',
            'Tebibit',
            'Petabit',
            'Pebibit',
            'Byte',
            'Kilobyte',
            'Kibibyte',
            'Megabyte',
            'MebiByte',
            'Gigabyte',
            'Gibibyte',
            'Terabyte',
            'Tebibyte',
            'Petabyte',
            'Pebibyte'
        ]

        self.options_comprimento = [
            'Quilômetro',
            'Metro',
            'Centímetro',
            'Milímetro',
            'Micrômetro',
            'Nanômetro',
            'Milha',
            'Jarda',
            'Pé',
            'Polegada',
            'Milha náutica'
        ]

        self.options_consumo_de_combustivel = [
            'Milha por galão americano',
            'Milha por galão imperial',
            'Quilômetro por litro',
            'Litro por 100 quilômetros'
        ]

        self.options_energia_mecanica = [
            'Joule',
            'Quilojoule',
            'Gram calorie',
            'Quilocaloria',
            'Watt-hora',
            'Quilowatt-hora',
            'Elétron-volt',
            'BTU',
            'therm (US)',
            'Pé-libra força'
        ]

        self.options_frequencia = [
            'Hertz',
            'Quilo-hertz',
            'Mega-hertz',
            'Giga-hertz'
        ]

        self.options_massa = [
            'Tonelada',
            'Quilograma',
            'Grama',
            'Miligrama',
            'Micrograma',
            'Tonelada de deslocamento',
            'Tonelada curta',
            'Stone',
            'Libra',
            'Onça'
        ]

        self.options_pressao = [
            'Atmosfera padrão',
            'Bar',
            'Pascal',
            'Psi',
            'Torr'
        ]

        self.options_temperatura = [
            'Grau Celsius',
            'Grau Fahrenheit',
            'Kelvin'
        ]

        self.options_tempo = [
            'Nanosegundo',
            'Microssegundo',
            'Milissegundo',
            'Segundo',
            'Minuto',
            'Hora',
            'Dia',
            'Semana',
            'Mês',
            'Ano-calendário',
            'Década',
            'Século'
        ]

        self.options_transmissao_de_dados = [
            'Bit por segundo',
            'Quilobit por segundo',
            'Quilobyte por segundo',
            'Kibibit por segundo',
            'Megabit por segundo',
            'Megabyte por segundo',
            'Mebibit por segundo',
            'Gigabit por segundo',
            'Gigabyte por segundo',
            'Gibibit por segundo',
            'Terabit por segundo',
            'Terabyte por segundo',
            'Tebibit por segundo'
        ]

        self.options_velocidade = [
            'Milha por hora',
            'Pés por segundo',
            'Metro por segundo',
            'Quilômetro por hora',
            'Nó'
        ]

        self.options_volume = [
            'Galão americano',
            'Quarto líquido americano',
            'Pinta americana',
            'Copo',
            'Onça líquida americana',
            'Colher de sopa americana',
            'Colher de chá americana',
            'Metro cúbico',
            'Litro',
            'Mililitro',
            'Galão imperial',
            'Quarto imperial',
            'Pinto imperial',
            'Xícara imperial',
            'Onça líquida imperial',
            'Colher de sopa imperial',
            'Colher de chá imperial',
            'Pé cúbico',
            'Polegada cúbica'
        ]

        self.options_area = [
            'Quilômetro quadrado',
            'Metro quadrado',
            'Milha quadrada',
            'Jarda quadrada',
            'Pé quadrado',
            'Polegada quadrada',
            'Hectare',
            'Acre'
        ]

        self.options_angulo = [
            'Grado',
            'Grau',
            'Mil angular',
            'Minuto de arco',
            'Radiano',
            'Segundo de arco'
        ]

    def change_options_menu(self, stringVar_drop_down_1, stringVar_drop_down_2, stringVar_drop_down_3, drop_down_2, drop_down_3):
        atual_option = stringVar_drop_down_1.get()

        if atual_option == 'Armazenamento de Dados':

            stringVar_drop_down_2.set('Byte')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Byte')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_armazenamento_de_dados:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Comprimento':

            stringVar_drop_down_2.set('Metro')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Metro')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_comprimento:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Consumo de Combustível':

            stringVar_drop_down_2.set('Quilômetro por litro')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Quilômetro por litro')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_consumo_de_combustivel:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Energia Mecânica':

            stringVar_drop_down_2.set('Joule')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Joule')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_energia_mecanica:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Frequência'[4]:

            stringVar_drop_down_2.set('Hertz')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Hertz')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_frequencia:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Massa':

            stringVar_drop_down_2.set('Quilograma')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Quilograma')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_massa:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Pressão':

            stringVar_drop_down_2.set('Pascal')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Pascal')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_pressao:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Temperatura':

            stringVar_drop_down_2.set('Grau Celsius')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Grau Celsius')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_temperatura:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Tempo':

            stringVar_drop_down_2.set('Segundo')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Segundo')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_tempo:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Transmissão de dados':

            stringVar_drop_down_2.set('Bit por segundo')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Bit por segundo')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_transmissao_de_dados:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Velocidade':

            stringVar_drop_down_2.set('Metro por segundo')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Metro por segundo')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_velocidade:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Volume':

            stringVar_drop_down_2.set('Litro')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Litro')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_volume:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Área':

            stringVar_drop_down_2.set('Metro quadrado')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Metro quadrado')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_area:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

        elif atual_option == 'Ângulo':

            stringVar_drop_down_2.set('Grau')
            drop_down_2['menu'].delete(0, 'end')

            stringVar_drop_down_3.set('Grau')
            drop_down_3['menu'].delete(0, 'end')

            for option in self.options_angulo:
                drop_down_2['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_2, option))
                drop_down_3['menu'].add_command(
                    label=option, command=tk._setit(stringVar_drop_down_3, option))

  • Cannot find any code that shows the selected item when item in menu two or three is selected. – acw1668 May 20 '21 at 03:23
  • i did exclud from the class converter, but the code is simillar to: label_formula['text'] = f'{stringVar_drop_down_2} -> {stringVar_drop_down_3}' – Jhosef Matheus May 21 '21 at 14:41
  • Both are `StringVar`, so need to use `.get()` to get the content. Also if you call this statement in the callback of `command` option, then you need to pass the callback name to `tk._setit()` as the 3rd argument as well. – acw1668 May 21 '21 at 15:20
  • thanks, bro but i resolve the problem from a different way, i did use ttk.Combobox – Jhosef Matheus May 21 '21 at 16:01

0 Answers0