0

In a tkinter window I would like to recall, for example, departure derived from a function (departurr is derived, through a function, from a combobox), but I get the error NameError: name 'departure' is not defined, because departure is only defined in function. I would like to call it up normally in the window whenever I need it.

if departure == London:
    print("Ok, correct example")

This is the executable code:

import tkinter as tk                    
from tkinter import ttk
import sqlite3
  
root = tk.Tk()
root.title("Tab Widget")
root.geometry('400x200')

conn = sqlite3.connect('cities.db')
cursor = conn.cursor()   

def fill_voyage():
    cursor.execute('SELECT DISTINCT voyage FROM All_voyage')
    result=[row[0] for row in cursor]
    return result

def combo_voyage(event=None):
    val = voyage.get()
    cursor.execute('SELECT departure_city||"-"||destination_city FROM departure_destination WHERE voyage = ?', (val,)) 
    values = [row[0] for row in cursor] 
    print(values)
    departure_destination['value'] = values
    return values

def combo_departure_destination(event=None):
    select_departure_destination = departure_destination.get()

    #THIS
    if select_departure_destination:
        departure,destination = select_departure_destination.split('-')
        print("departure", departure)
        print("destination", destination)

voyage=ttk.Combobox(root, width = 25)
voyage['value'] = fill_voyage()
voyage.bind('<<ComboboxSelected>>', combo_voyage)
voyage.place(x=42, y=8)
voyage.set("Choose travel country")

departure_destination=ttk.Combobox(root, width = 25)
departure_destination.place(x=42, y=48)
departure_destination.bind('<<ComboboxSelected>>', combo_departure_destination)
departure_destination.set("Choose departure-destination")

root.mainloop()

How can I solve?

Jas_99
  • 215
  • 10
  • One calls a function, not a variable. A variable is accessed, not called. No, you cannot access a local variable outside of a function. You must make it global. – DYZ Mar 08 '22 at 01:43

0 Answers0