0

我正在用python制作一个单位转换器,它有3个选项菜单,其中一个选项菜单包含您要转换的事物的类型(压力、温度、时间、数据传输、速度、面积等),选项菜单二和三是根据选项菜单一的,如果您在选项菜单一中输入要转换某个速度值的菜单,则菜单二和三将仅显示速度值。

当我运行代码并从菜单二和三中选择任何选项时,它会打印我从两者中选择的值,但是当我从菜单一中选择任何选项时,它不再打印我选择的内容。

我不知道如何解决它。

代码如下:

来自 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()

来自类转换器的代码,它是我的应用程序的后端:

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))

4

0 回答 0