通過選擇兩個組合框(結合系結),我想從 Table1 中提取一個欄位的 ID 并將其插入到 Table2 中。我在基于組合框提取欄位時沒有問題,但在基于 2 個組合框提取欄位時遇到問題,因為它們相互組合。問題僅在于def id_rounds()函式。
我有兩個組合框:一個是我選擇“錦標賽”的名稱,另一個是我選擇回合數(每場比賽從 1 到 38 個不同的回合)。要選擇錦標賽 ID 必須匹配的錦標賽,我使用組合框 combo_Tournaments 和函式 def combo_tournaments;而選擇輪數我使用組合框combo_Rounds和combo_rounds函式。通過選擇錦標賽和/或回合,還會自動輸入相關 ID(以及實際資料)。所以每個組合框各放 2 個東西,總共 4 個。
這是資料庫:
CREATE TABLE "All_Tournament" (
"ID_Tournament" INTEGER,
"Tournament" TEXT,
PRIMARY KEY("Tournament" AUTOINCREMENT)
);
CREATE TABLE "All_Round" (
"ID_Round" INTEGER,
"Number_Round" INTEGER,
"ID_Tournament" INTEGER,
PRIMARY KEY("ID_Round" AUTOINCREMENT),
);
問題:目前,當我撰寫函式def id_rounds()的代碼時,所選回合的 ID 已保存,但與錦標賽組合框中所選錦標賽的 ID 沒有完全對應。問題是每個錦標賽都由 38 個不同的回合組成,因此在 All_Round 表中,從 1 到 38 的數字重復了幾次,每個都對應于錦標賽 ID。例如意甲第 1 輪到第 38 輪;乙級聯賽從 1 輪到 38 輪;英超聯賽從 1 輪到 38 輪。所以想輸入Tournament對應的單個Round的ID(與錦標賽有關),因為每個錦標賽有1到38個回合,所以每個錦標賽有很多不同的“1到38個回合”。
#Combobox Tournament
lbl_Tournament = Label(root, text="Tournament", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Tournament.place(x=6, y=60)
combo_Tournaments = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=campionato, state="readonly")
combo_Tournaments.place(x=180, y=60)
combo_Tournaments.set("Select")
combo_Tournaments['values'] = combo_tournaments()
combo_Tournaments.bind('<<ComboboxSelected>>', combo_teams)
lbl_Rounds = Label(root, text="Rounds", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Rounds.place(x=600, y=60)
combo_Rounds = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=rounds, state="readonly")
combo_Rounds.place(x=680, y=60)
combo_Rounds.set("Select")
combo_Rounds['values'] = combo_campionati()
combo_Tournaments.bind('<<ComboboxSelected>>', combo_rounds, add=True)
def combo_tournaments():
tournaments = combo_tournaments.get()
cursor.execute('SELECT Tournament FROM All_Tournament')
result=[row[0] for row in cursor]
return result
def id_tournaments():
tournaments = combo_tournaments.get()
cursor.execute('SELECT ID_Tournament FROM All_Tournament WHERE Tournament=?',(tournaments,))
result=[row[0] for row in cursor]
return result[0]
def combo_rounds(event=None):
rounds = combo_rounds.get()
cursor.execute('SELECT Number_Round From All_Round WHERE ID_Tournament')
result=[row[0] for row in cursor]
combo_Rounds['value'] = result
return result
#THE PROBLEM IS HERE
def id_rounds():
rounds = combo_rounds.get()
cursor.execute('SELECT ID_Round FROM All_Round WHERE Number_Round=? AND Tournament=?',(rounds, tournaments))
result=[row[0] for row in cursor]
return result[0]
def combo_teams(event=None):
tournaments = combo_tournaments.get()
cursor.execute('SELECT s.Name_Teams FROM All_Teams s, All_Tournament c WHERE s.ID_Tournament=c.ID_Tournament AND c.Tournament = ?', (tournaments,))
result=[row[0] for row in cursor]
combo_Teams_1['values'] = result
combo_Teams_2['values'] = result
return result
WHAT DO I WANT TO GET? So I would like to obtain for example that: if from the Tournament combobox I select Serie A and then Round 1, in the Results table the ID of Round 1 should be entered but corresponding to Serie A. Or, another example, if from the Tournament combobox I select Serie B and then Round 1, the ID of Round 1 should be entered in the Results table but corresponding to Serie B.
QUESTION: How can I fix the function def id_rounds and which inserts the number of the Round in correspondence (in relation) to the tournament? Currently I only enter the ID of the selected Round in the combobox without matching the championship chosen in the tournament combobox.
uj5u.com熱心網友回復:
以下是根據我的理解修改后的代碼:
def combo_tournaments():
cursor.execute('SELECT Tournament FROM All_Tournament')
result=[row[0] for row in cursor]
return result
def combo_rounds(event=None):
# get all Number_Round for selected tournament
cursor.execute('''
SELECT Number_Round From All_Round r, All_Tournament t
WHERE r.ID_Tournament = t.ID_Tournament AND Tournament = ?''', (campionato.get(),))
result=[row[0] for row in cursor]
combo_Rounds['value'] = result # update combo_Rounds
rounds.set('Select') # reset Rounds selection
return result
def id_rounds(event=None):
# get the ID_Round based on selected tournament and Number_Round
cursor.execute('''
SELECT ID_Round FROM All_Round r, All_Tournament t
WHERE r.ID_Tournament = t.ID_Tournament AND Number_Round = ? AND Tournament = ?''',
(rounds.get(), campionato.get()))
result = cursor.fetchone()
if result:
print(result[0])
return result[0]
return None
...
campionato = StringVar()
rounds = StringVar()
#Combobox Tournament
lbl_Tournament = Label(root, text="Tournament", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Tournament.place(x=6, y=60)
combo_Tournaments = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=campionato, state="readonly")
combo_Tournaments.place(x=180, y=60)
combo_Tournaments.set("Select")
combo_Tournaments['values'] = combo_tournaments()
combo_Tournaments.bind('<<ComboboxSelected>>', combo_rounds)
lbl_Rounds = Label(root, text="Rounds", font=("Calibri", 11), bg="#E95420", fg="white")
lbl_Rounds.place(x=600, y=60)
combo_Rounds = ttk.Combobox(root, font=("Calibri", 11), width=30, textvariable=rounds, state="readonly")
combo_Rounds.place(x=680, y=60)
combo_Rounds.set("Select")
combo_Rounds.bind('<<ComboboxSelected>>', id_rounds)
...
請注意,我已使用campionato和rounds( StringVar) 來獲取選定的錦標賽和Number_Round。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/311412.html
標籤:python sql python-3.x sqlite tkinter
上一篇:如何將資料從一列復制到另一列?
