我試圖創建一個簡單的組合式組合框,從資料庫Sqlite和Python 3.x中提取資料。如果我打開組合框1 "Nation/Country",作為一個例子,我想在組合框2 "City "中顯示相關城市。
我得到這個錯誤
return self.tk.call (self._w, "current", newindex)
_tkinter.TclError: 索引 0 out of range
即使以前沒有得到,組合框也沒有很好地作業,因為如果我打開英格蘭,收到倫敦、利物浦、曼徹斯特等,我在組合框1 "國家/國家 "中選擇其他國家,我將總是收到英格蘭的城市。例如,我打開法國,仍然收到倫敦、利物浦、曼徹斯特(而不是巴黎、馬賽、波爾多等)。
我怎樣才能解決?你能用我的代碼告訴我答案嗎?我是Python的新手,如果你只從理論上向我解釋這個問題,我可能無法理解。謝謝
。#combobox nation
lbl_Nation = Label(root, text="City", font=( "Calibri", 11), bg="#a8dd71", fg="白色")
lbl_Nation.place(x=6, y=60)
combo_Nation = ttk. Combobox(root, font=("Calibri", 11), width=30, state=" readonly")
combo_Nation.place(x=180, y=60)
combo_Nation.set("選擇國家")
def combo_nation():
cursor.execute('SELECT Nation FROM All_Nation')
result=[row[0] for row in cursor ]
return result
combo_Nation['value'] = combo_nation()
combo_Nation.set("Seleziona")
###############################################################
#combobox城市
lbl_City = Label(root, text="City", font=("Calibri", 11), bg="白色", fg="黑色")
lbl_City.place(x=6, y=110)
combo_City = ttk. Combobox(root, font=("Calibri", 11), width=30, state="readonly" )
combo_City.place(x=180, y=110)
combo_City.set("選擇城市")
def combo_city():
val = combo_City.get()
cursor.execute('SELECT s.City FROM All_Nation s, All_Nation c WHERE s.ID_Nation=c.ID_Nation AND c.City = ?, (val,)
result=[row[0] for row in cursor ]
combo_City['value'] = result)
combo_City.current(0)
return result[/span
combo_City['value'] = combo_City()
combo_City.set("選擇")
combo_Nation.bind('<<ComboboxSelected>>'/span>, combo_city)
UPDATE N.1
CREATE TABLE "All_Nation" (
"ID_Nation" INTEGER,
"國家" TEXT,
PRIMARY KEY("ID_Nation" AUTOINCREMENT)
);
創建 表 "城市" (
"ID_City" INTEGER,
"城市" TEXT,
"ID_Nation" INTEGER,
FOREIGN KEY("ID_Nation") REFERENCES "All_Nation"("ID_Nation") On DELETE NO ACTION ON UPDATE NO ACTION。
PRIMARY KEY("ID_Sq" AUTOINCREMENT)
);
uj5u.com熱心網友回復:
基于所提供的表資訊,在combo_city()中使用的SQL將不會回傳任何記錄。
下面是一個基于發布的代碼的例子:
import tkinter as tk
from tkinter import ttk
import sqlite3
cnx = sqlite3.connect(' nations.db')
cursor = cnx.cursor()
def combo_nation()。
cursor.execute('SELECT Nation FROM All_Nation')
result = [row[0] for row in cursor)
return result
def combo_city(event)。
nation = combo_Nation.get() # get the selected nation.
cursor.execute('SELECT City FROM All_Nation n, CITY c WHERE n.ID_Nation = c.ID_Nation AND n.Nation = ?, (nation,)
result = [row[0] for row in cursor ]
combo_City['values'] = result
combo_City.set('Select City')
root = tk.Tk()
root.geometry('600x400')
root.config(bg='white')
# Nation[/span]。
tk. Label(root, text="Nation/Country", font=("Calibri", 11), bg="white", fg="black")。) place(x=6, y=60)
combo_Nation = ttk.Combobox(root, font=("Calibri", 11), width=30, state="readonly")
combo_Nation.place(x=180, y=60)
combo_Nation.set("Select Nation"/span>)
combo_Nation[' values'] = combo_nation()
combo_Nation.set("Seleziona")
combo_Nation.bind('<<ComboboxSelected>>'/span>, combo_city)
# City"City"/span>, font=("Calibri"/span>, 11), bg="白色", fg="黑色")。) place(x=6, y=110)
combo_City = ttk.Combobox(root, font=("Calibri", 11), width=30, state="readonly")
combo_City.place(x=180, y=110)
combo_City.set("選擇城市")
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/309793.html
標籤:
