bin komplett Python-Newbie. Hatte war viel mit Java gemacht. Aber bei Python bin ich total lost. Ich schreibe gerade an mein allererstes Projekt. Soll ein Unit Converter sein. Ich bekomme folgende Fehler: "_tkinter.TclError: bad state "readonly": must be active, disabled, or normal". Konnte fast nix dazu auf google finden. Kann jemand mit bitte helfen bzw erklaeren, was schief lief?
Code: Alles auswählen
import tkinter as tk
from tkinter import *
from tkinter import ttk
import Calculation
class UnitConvertion(tk.Tk):
def __init__(self):
super().__init__()
self.caldict = Calculation.calculation_dictionaries
# window configuration
self.geometry("550x250")
self.title("Unit Converter")
self.configure(bg="light grey")
self.resizable(height=False, width=False)
# labels
self.scales_lbl = Label(self, text="Choose scales", bg="black", fg="white", width=15)
self.scales_lbl.grid(column=0, row=0, sticky=W, padx=4, pady=4)
self.input_value_lbl = Label(self, text="Input value", bg="black", fg="white", width=15)
self.input_value_lbl.grid(column=0, row=1, sticky=W, padx=4, pady=4)
self.convert_from_lbl = Label(self, text="Convert from", bg="black", fg="white", width=15)
self.convert_from_lbl.grid(column=0, row=2, sticky=W, padx=4, pady=4)
self.convert_to_lbl = Label(self, text="Convert to", bg="black", fg="white", width=15)
self.convert_to_lbl.grid(column=2, row=2, sticky=W, padx=4, pady=4)
self.result = StringVar()
self.result_lbl = Label(self, textvariable=self.result, state="readonly")
# Entry
self.unit_value = DoubleVar()
self.unit_entry = Entry(self, textvariable=self.unit_value, width=23)
self.unit_entry.grid(column=1, row=1, sticky=W, padx=4, pady=4)
# comboboxes
self.scale_string = StringVar()
self.scales_options_cbx = ttk.Combobox(self, textvariable=self.scale_string_cbx, state="readonly",
values=tuple([available_scales.upper() for available_scales in self.caldict.keys()]))
self.scales_options_cbx.bind("<<SelectedCombobox>>", self.setUnits)
self.scales_options_cbx.grid(column=1, row=0, sticky=E, padx=4, pady=4)
self.unit_value_from = StringVar()
self.from_options_cbx = ttk.Combobox(self, textvariable=self.unit_value_from, state="readonly")
self.from_options_cbx.grid(column=1, row=2, sticky=E, padx=4, pady=4)
self.unit_value_to = StringVar()
self.to_options_cbx = ttk.Combobox(self, textvariable=self.unit_value_to, state="readonly")
self.to_options_cbx.grid(column=4, row=2, sticky=E, padx=4, pady=4)
# Buttons
self.convert_btn = Button(self, text="Calculate", width=15, command=self.calculation())
self.convert_btn.grid(row=3, padx=4, pady=4)
def setUnits(self):
self.unit_from["value"] = tuple(self.caldict.get().upper().keys())
self.unit_to["value"] = tuple(self.caldict.get().upper().keys())
def calculation(self):
# TODO
pass
if __name__ == "__main__":
application = UnitConvertion()
application.mainloop()
Code: Alles auswählen
calculation_dictionaries = {
"time": {
"hours": {
"seconds": lambda x: x * 60 * 60,
"minutes": lambda x: x * 60,
"hours": lambda x: x,
},
"minutes": {
"seconds": lambda x: x * 60,
"minutes": lambda x: x,
"hours": lambda x: x / 60,
},
"seconds": {
"seconds": lambda x: x,
"minutes": lambda x: x / 60,
"hours": lambda x: x / (60 * 60),
}
},
...
}
if __name__ == "__main__":
try:
# format for decimal points
print("{0:.6f}".format(
calculation_dictionaries["length"]["millimeters"]["centimeters"]["decimeters"]["meters"](4)))
except TypeError:
pass
except KeyError:
pass
for cd in calculation_dictionaries.keys():
print(cd.capitalize())
print(tuple(calculation_dictionaries[cd].keys()))