Tkinter Fehler finden

Fragen zu Tkinter.
Antworten
Maxus
User
Beiträge: 2
Registriert: Montag 21. März 2022, 08:19

Hallo,

ich bin ein Schüler und bin gerade dabei ein Auswahlmenü mit einem GUI Tkinter Interface zu programmieren. Der erste Code, ist der Code, so wie das programm hinterher laufen soll und welches ich mir zur hilfe nehme, und beim zweiten Code weiß ich nicht wo der felhler liegt. Vielen dank schon mal im Voraus für ihre Unterstützung.

1. Code

Code: Alles auswählen

destination = input("Enter your destination (Edinburgh or Cardiff): ")
travelclass= input("Enter your class (business or economy): ")
meal = input("Do you want on-bord meal(yes or no)? ")
ticket = input("One way or with return(One way or return)? ")

price = 0.00
if destination == "Edinburgh":
    if travelclass == "business":
        if ticket == "return":
            price = 178
        elif ticket == "One way":
            price = 87
    elif travelclass == "economy":
        if ticket == "return":
            if meal == "yes":
                price = 52 + 18
            elif meal == "no":
                price = 52
        elif ticket == "One way":
            if meal == "yes":
                price = 32 + 12
            elif meal == "no":
                price = 32
                price = 5
elif destination == "Cardiff":
    if travelclass == "business":
        if ticket == "return":
            price = 138
        elif ticket == "One way":
            price = 72
    elif travelclass == "economy":
        if ticket == "return":
            if meal == "yes":
                price = 50 + 18
            elif meal == "no":
                price = 50
        elif ticket == "One way":
            if meal == "yes":
                price = 36 + 12
            elif meal == "no":
                price = 36
                
                
print("Your travel costs", price, "£")
2. Code

Code: Alles auswählen

from tkinter import*

window=Tk()
window.title = "Train booking"

price = 0.00
destination_label = Label(window, width=30, height=1, text="To")
destination_label.grid(row=0, column=1)

travelclass_label = Label(window, width=30, height=1, text="Business or economy class")
travelclass_label.grid(row=3, column=1)

meal_label = Label(window, width=30, height=1, text="Do you want meal?")
meal_label.grid(row=6, column=1)

ticket_label = Label(window, width=30, height=1, text="One way or with return?")
ticket_label.grid(row=9, column=1)

destination = StringVar()

edinburgh = Radiobutton(window, text="Edinburgh", variable = destination, value = "Edinburgh")
edinburgh.grid(row=1, column=1)
edinburgh.select()
cardiff = Radiobutton(window, text="Cardiff", variable = destination, value = "Cardiff")
cardiff.grid(row=2, column=1)

travelclass = StringVar()

businessclass = Radiobutton(window, text="Businessclass", variable = travelclass, value = "Businessclass")
businessclass.grid(row=4, column=1)
businessclass.select()
economyclass = Radiobutton(window, text="Economyclass", variable = travelclass, value = "Economyclass")
economyclass.grid(row=5, column=1)

meal = StringVar()

yes_meal = Radiobutton(window, text="Yes", variable = meal, value = "Yes")
yes_meal.grid(row=7, column=1)
yes_meal.select()
no_meal = Radiobutton(window, text="No", variable = meal, value = "No")
no_meal.grid(row=8, column=1)

ticket = StringVar()

return_ticket = Radiobutton(window, text="Return", variable = ticket, value = "Return")
return_ticket.grid(row=10, column=1)
return_ticket.select()
one_way_ticket = Radiobutton(window, text="One way", variable = ticket, value = "One way")
one_way_ticket.grid(row=11, column=1)
 
    

    
def change_text(return_ticket, one_way_ticket, no_meal, yes_meal, economyclass, businessclass, cardiff, edinburgh):
    if destination == "Edinburgh":
        if travelclass == "Business":
            if ticket() == "Return":
                price = 178
            elif ticket() == "One way":
                price = 87
        elif travelclass == "Economy":
            if ticket() == "Return":
                if meal == "Yes":
                    price = 52 + 18
                elif meal == "No":
                    price = 52
            elif ticket() == "One way":
                 if meal == "Yes":
                     price = 32 + 12
                 elif meal == "No":
                    price = 32
    elif destination == "Cardiff":
        if travelclass == "Business":
            if ticket == "Return":
                price = 138
            elif ticket == "One way":
                price = 72
        elif travelclass == "Economy":
            if ticket == "Return":
                if meal == "Yes":
                    price = 50 + 18
                elif meal == "No":
                    price = 50
            elif ticket == "One way":
                if meal == "Yes":
                    price = 36 + 12
                elif meal == "No":
                    price = 36
                        



submit = Button(window, text="Submit", width=10, bg="red", command=change_text(return_ticket, one_way_ticket, no_meal, yes_meal, economyclass, businessclass, cardiff, edinburgh))
submit.grid(row=12, column=1)

total_costs = Label(window, width=30, height=1, text= ("Your travel costs", price, "£"))
total_costs.grid(row=13, column=1)



window.mainloop()
Sirius3
User
Beiträge: 17741
Registriert: Sonntag 21. Oktober 2012, 17:20

Beim ersten Code fehlt jegliche Fehlerbehandlung: falls `destination` oder `travelclass` nicht den vorhandenen Optionen entsprechen wird einfach nur `price = 0` ausgeführt, was zu sehr günstigen Tickets zu sehr exotischen Zielen führt.

Statt langer if-Kaskaden benutzt man in Python Wörterbücher.

Code: Alles auswählen

TICKET_PRICES = {
    "Edinburgh": {
        "business": {
            "return": 178,
            "One way": 87,
        },
        "economy": {
            "return": (52 + 18, 52), # with meal or not
            "One way": (32 + 12, 5), # super special economy with no meal
        }
    },
    "Cardiff": {
        "business": {
            "return": 138,
            "One way": 72,
        },
        "economy": {
            "return": (50 + 18, 50),
            "One way": (36 + 12, 36),
        }
    }
}

destination = input("Enter your destination (Edinburgh or Cardiff): ")
travelclass= input("Enter your class (business or economy): ")
ticket = input("One way or with return(One way or return)? ")

try:
    price = TICKET_PRICES[destination][travelclass][ticket]
except KeyError:
    print("destination or class not available")
else:
    if isinstance(price, tuple):
        # with meal or not
        meal = input("Do you want on-bord meal (yes or no)? ")
        assert meal in ["yes", "no"]
        price = price[meal == "no"]
    print(f"Your travel costs {price}£")
Im zweiten Code hast Du dann das Problem nicht mehr, dass die Auswahlmöglichkeiten vor `destination` oder `travelclass` doppelt im Code vorkommen müssen, da Du das Wörterbuch auch zum Füllen der Radiobuttons benutzen kannst.

*-Importe benutzt man nicht, tkinter wird üblicherweise als `import tkinter as tk` importiert.
Globale Variablen benutzt man nicht, der ganze Code muß also in Funktionen verlagert werden.
command bei Button erwartet ein aufrufbares Objekt, und nicht den Rückgabewert einer Funktion. Wenn man keine Klassen schreibt (was man bei GUIs aber dringend tun sollte), muß man alles Parameter per functools.partial an die Event-Funktion binden, und das dann command übergeben.
Da dieser Fehler sehr häufig gemacht wird, handelt jeder dritte Beitrag in diesem Forum von diesem Problem. Einfach mal die Post durchstöbern.
`destination`, `travelclass` oder `ticket` ist StingVar-Instanzen, diese sind niemals gleich Strings und können auch nicht aufgerufen werden.
Mit dem Ergebnis `price` tust Du nichts, Du mußt aber noch das Label aktualisieren.
Antworten