Display elements of a combobox in another combobox, but separated without the hyphen (-)

Viewed 64

There are 3 comboboxes. The purpose is to select the country of the trip in the first combobox (travel_country), then select departure and destination in the second combobox (departure_destination). ALL OK so far, everything works correctly. In fact, in the second combobox, departure and destination are displayed with a dash (-) like this: London-Liverpool.

Now I have added a third combobox (two_cities) which aims to show the departure and destination selected in the previous combobox, but with separate and separate departure and destination (without the hyphen). For example, if in the previous combobox (departure_destination) I selected London-Liverpool, now in the combobox two_cities I would like to display London and then under it Liverpool (therefore 2 items): London, Liverpool

enter image description here

I created this function where I thought about using the city_departure and city_destination global variables (maybe my idea of ​​using global functions is wrong and there are better ways), but it doesn't work:

def two_cities_function(event=None):
    select_departure_destination = departure_destination.get()
    global city_departure
    global city_destination
    city_departure, city_destination = select_departure_destination.split('-')
    one_element = [city_departure, city_destination]
    two_cities['value'] = one_element
    return one_element

PROBLEM: I have no errors, but the two_cities combobox continues to be empty and not showing anything

UPDATE: I noticed that by replacing sql with internal variables (not sql), my code works correctly. So I think maybe it is a Sql problem, but I specify that the sql of the 2 comboboxes is working correctly. The problem is only the two_cities combobox.

Can you help me? How can I display the elements (separated, without hyphen) in two_cities? Thank you

Code:

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

conn = sqlite3.connect('/home/santiago/desktop/database.db')
cursor = conn.cursor()   

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

def combo_travel_country(event=None):
    global travel_country
    val = travel_country.get()
    cursor.execute('SELECT team_home||"-"||team_away FROM BASE_ProssimaGiornata WHERE  Campionato = ?', (val,))
    values = [row[0] for row in cursor] 
    print(values)
    global departure_destination
    departure_destination['value'] = values
    return values

def seleziona(event=None):
    select_departure_destination = departure_destination.get()
    
    if select_departure_destination:
        global city_departure
        global city_destination
        city_departure,city_destination = select_departure_destination.split('-')
        print("departure", city_departure)
        print("destination", city_destination)

def two_cities_function(event=None):
    select_departure_destination = departure_destination.get()
    global city_departure
    global city_destination
    city_departure, city_destination = select_departure_destination.split('-')
    one_element = [city_departure, city_destination]
    two_cities['value'] = one_element
    return one_element


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

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

two_cities=ttk.Combobox(root, width = 18)
two_cities.place(x=42, y=85)
two_cities.set("two cities")
two_cities.bind('<<ComboboxSelected>>', two_cities_function)

root.mainloop()
1 Answers

You want something like this?

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

# conn = sqlite3.connect('/home/santiago/desktop/database.db')
# cursor = conn.cursor()   

def fill_voyage():
    # cursor.execute('SELECT DISTINCT campionato FROM BASE_ProssimaGiornata')
    # result=[row[0] for row in cursor]
    # return result
    return ["a", "b", "c"]


def combo_travel_country(event=None):
    global travel_country
    val = travel_country.get()
    # cursor.execute('SELECT team_home||"-"||team_away FROM BASE_ProssimaGiornata WHERE  Campionato = ?', (val,))
    # values = [row[0] for row in cursor] 
    values = ["x-abc", "y-def", "z-ghi"]
    print(values)
    global departure_destination
    departure_destination['value'] = values
    return values

def seleziona(event=None):
    select_departure_destination = departure_destination.get()
    
    if select_departure_destination:
        global city_departure
        global city_destination
        squadra_sinistra,squadra_destra = select_departure_destination.split('-')
        print("departure", city_departure)
        print("destination", city_destination)

def two_cities_function(event=None):
    select_departure_destination = departure_destination.get()
    global city_departure
    global city_destination
    city_departure, city_destination = select_departure_destination.split('-')
    one_element = [city_departure, city_destination]
    two_cities['value'] = one_element
    return one_element


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

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

two_cities=ttk.Combobox(root, width = 18)
two_cities.place(x=42, y=85)
departure_destination.bind('<<ComboboxSelected>>', two_cities_function)
two_cities.set("two cities")

root.mainloop()

enter image description here

Related