我为每个客户都有多个约定,所以我想做的是更容易选择合适的约定。我试图通过两个组合框来实现这一点,一个显示客户列表,另一个显示参与列表。然而,我希望根据选择的客户来缩小参与列表。我知道我正确地传递了参与下拉列表的参数,但当我尝试设置engagement1['values']=engagement_dropdown((时。我收到TypeError:engagement_dropdown((缺少1个必需的位置参数:"client_dropdown"。当我尝试在engagement_dropdown中添加client_dropdown时,我会得到pyodbc。编程错误:("SQL包含1个参数标记,但提供了72个参数","HY000"(。我错过了什么?
提前感谢!
from tkinter import *
from tkinter import ttk
import sys
import os
import backend
import pyodbc
from functools import partial
global conn
conn = pyodbc.connect(
Driver = '{ODBC Driver 17 for SQL Server}',
Server = 'server',
Database = 'db',
Trusted_Connection = 'Yes')
def client_dropdown():
cur = conn.cursor()
cur.execute("SELECT [ClientName] FROM TimeEntryDB.dbo.DimClientEngagements GROUP BY [ClientName] ORDER BY [ClientName] ASC")
data = []
for client_list in cur.fetchall():
data.append(client_list[0])
return data
def engagement_dropdown(client_dropdown):
cur = conn.cursor()
cur.execute("SELECT [EngagementName] FROM TimeEntryDB.dbo.DimClientEngagements WHERE [ClientName] = ?", (client_dropdown()))
data = []
for engagement_list in cur.fetchall():
data.append(engagement_list[0])
return data
screen = Tk()
screen.geometry("500x300")
screen.title("Time Entry Application")
heading = Label(text = "Review and Approve Time Entries and Timesheets", bg = "blue", fg = "white", width = "500", height = "2")
heading.pack()
#Client field
engagement_label = Label(text = "Client",)
engagement_label.place(x = 15, y = 50)
selected_client = StringVar()
client1 = ttk.Combobox(screen, width = 20, textvariable = selected_client, state = 'readonly')
client1['values'] = client_dropdown()
client1.place(x= 15, y = 70)
#Engagement field
engagement_label = Label(text = "Engagement",)
engagement_label.place(x = 200, y = 50)
selected_engagement = StringVar()
engagement1 = ttk.Combobox(screen, width = 40, textvariable = selected_engagement, state = 'readonly')
engagement1['values'] = engagement_dropdown()
engagement1.place(x= 200, y = 70)
screen.mainloop()
我认为您的问题是将参数传递到函数中以供在函数中使用,以及在函数内部使用变量。你写函数的方式,你问你它需要一个论点,而你没有在帖子中传递一个。我已经修改了函数调用,以便在声明的参数中传递一个值,并在函数中使用它。这应该可以解决您的问题。
from tkinter import *
from tkinter import ttk
import sys
import os
import backend
import pyodbc
from functools import partial
global conn
conn = pyodbc.connect(
Driver = '{ODBC Driver 17 for SQL Server}',
Server = 'server',
Database = 'db',
Trusted_Connection = 'Yes')
def client_dropdown():
cur = conn.cursor()
cur.execute("SELECT [ClientName] FROM TimeEntryDB.dbo.DimClientEngagements GROUP BY [ClientName] ORDER BY [ClientName] ASC")
data = []
for client_list in cur.fetchall():
data.append(client_list[0])
return data
def engagement_dropdown(client_val):
cur = conn.cursor()
sql_val = "SELECT [EngagementName] FROM TimeEntryDB.dbo.DimClientEngagements WHERE [ClientName] = " + str(client_val)
cur.execute(sql_val)
data = []
for engagement_list in cur.fetchall():
data.append(engagement_list[0])
return data
screen = Tk()
screen.geometry("500x300")
screen.title("Time Entry Application")
heading = Label(text = "Review and Approve Time Entries and Timesheets", bg = "blue", fg = "white", width = "500", height = "2")
heading.pack()
#Client field
engagement_label = Label(text = "Client",)
engagement_label.place(x = 15, y = 50)
selected_client = StringVar()
client1 = ttk.Combobox(screen, width = 20, textvariable = selected_client, state = 'readonly')
client1['values'] = client_dropdown()
client1.place(x= 15, y = 70)
#Engagement field
engagement_label = Label(text = "Engagement",)
engagement_label.place(x = 200, y = 50)
selected_engagement = StringVar()
engagement1 = ttk.Combobox(screen, width = 40, textvariable = selected_engagement, state = 'readonly')
engagement1['values'] = engagement_dropdown(selected_client)
engagement1.place(x= 200, y = 70)
screen.mainloop()