'Problem when printing text left and right of a dash with Split
I have a "voyage" combobox that activates a second "departure_destination" combobox. In the second combobox I see the travel destinations separated by a dash, for example New York-Los Angeles, London-Liverpool, Paris-Montpellier
I would like to print the city to the left of the dash and the city to the right of the dash separately, then departure and destination. For example by selecting London-Liverpool, I would like to print departure: London and destination: Liverpool. I would like to print without creating a button
The problem is that when I try to print, it prints Choose travel country (and not the city to the left of the dash), while if I try to print the city to the right of the dash, I get the error: IndexError: list index out of range
I think the problem is here (because the comboboxes work correctly and get the records from the database correctly)
#PRINT
select_departure_destination = departure_destination.get()
if select_departure_destination:
#City to the left of the dash
departure = select_departure_destination.split('-')[0]
print("departure", departure)
#City to the right of the dash
destination = select_departure_destination.split('-')[1]
print("destination", destination)
The complete executable code is:
import tkinter as tk
from tkinter import ttk
import sqlite3
root = tk.Tk()
root.title("Tab Widget")
root.geometry('400x200')
conn = sqlite3.connect('database')
cursor = conn.cursor()
#COMBOBOX
def combo_voyage():
cursor.execute('SELECT DISTINCT voyage FROM All_voyage') #for example United States, Great Britain, France
result=[row[0] for row in cursor]
return result
def combo_departure_destination(event=None):
val = voyage.get()
cursor.execute('SELECT departure_city||"-"||destination_city FROM departure_destination WHERE voyage = ?', (val,)) #for example New York-Los Angeles, London-Liverpool, Paris-Montpellier
values = [row[0] for row in cursor]
departure_destination['value'] = values
return values
voyage=ttk.Combobox(root, width = 25)
voyage['value'] = combo_voyage()
voyage.bind('<<ComboboxSelected>>', combo_departure_destination)
voyage.place(x=42, y=8)
voyage.set("Choose travel country")
departure_destination=ttk.Combobox(root, width = 25)
#REMOVE departure_destination['value'] = combo_partita()
departure_destination.place(x=42, y=48)
departure_destination.set("Choose departure-destination")
#PRINT
select_departure_destination = departure_destination.get()
if select_departure_destination:
#City to the left of the dash
departure = select_departure_destination.split('-')[0]
print("departure", departure)
#City to the right of the dash
destination = select_departure_destination.split('-')[1]
print("destination", destination)
root.mainloop()
Solution 1:[1]
I'm a little disturbed that you couldn't build this function based on what you already had. Notice that I have renamed your combo_departure_destination to combo_voyage, since it is handling the voyage box, and have moved your "#PRINT" code into combo_departure_destination.
I'm also not sure why you are printing the result, since the application is a GUI function. You would usually store the answer in a text box in the GUI.
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()
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
