请直接给我修改好的代码
时间: 2024-03-06 08:49:12 浏览: 11
好的,以下是修改后的代码,可以直接替换原来的代码:
```
import pyodbc
import tkinter as tk
def get_history():
# 连接数据库
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=DESKTOP-JM5K5CS;DATABASE=bank;UID=sa;PWD=1')
# 获取游标
cursor = conn.cursor()
# 查询数据
account_number = entry_account_number.get()
password = entry_password.get()
transaction_date = entry_start_date.get()
transaction_type = transaction_var.get()
# 构造 SQL 查询语句
if transaction_type == '所有':
sql = "SELECT * FROM Transactions WHERE account_number = ?"
else:
sql = "SELECT * FROM Transactions WHERE account_number = ? AND transaction_type = ?"
# 查询数据并在文本框中显示
if transaction_type == '所有':
cursor.execute(sql, account_number)
else:
cursor.execute(sql, account_number, transaction_type)
rows = cursor.fetchall()
text_history.delete(1.0, tk.END)
for row in rows:
text_history.insert(tk.END, f"交易日期:{row[2]}, 交易方式:{row[3]}, 交易金额:{row[4]}, 备注:{row[5]}\n")
def on_transaction_type_changed(*args):
# 根据选择的交易方式调用 get_history() 函数
get_history()
# 创建窗口
window = tk.Tk()
window.title('查询历史记录')
# 创建标签和输入框
label_account_number = tk.Label(window, text='卡号:')
label_account_number.grid(row=0, column=0)
entry_account_number = tk.Entry(window)
entry_account_number.grid(row=0, column=1)
label_password = tk.Label(window, text='密码:')
label_password.grid(row=1, column=0)
entry_password = tk.Entry(window, show='*')
entry_password.grid(row=1, column=1)
label_start_date = tk.Label(window, text='交易日期:')
label_start_date.grid(row=2, column=0)
entry_start_date = tk.Entry(window)
entry_start_date.grid(row=2, column=1)
label_transaction_type = tk.Label(window, text='交易方式:')
label_transaction_type.grid(row=3, column=0)
transaction_var = tk.StringVar(value='所有')
transaction_var.trace('w', on_transaction_type_changed)
transaction_options = [
'所有',
'存款',
'取款',
'转入',
'转出'
]
transaction_menu = tk.OptionMenu(window, transaction_var, *transaction_options)
transaction_menu.grid(row=3, column=1)
label_password = tk.Label(window, text='备注:')
label_password.grid(row=4, column=0)
entry_password = tk.Entry(window, show='*')
entry_password.grid(row=4, column=1)
# 创建按钮和文本框
text_history = tk.Text(window)
text_history.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
button_search = tk.Button(window, text='查询', command=get_history)
button_search.grid(row=6, column=1)
window.mainloop()
```
这个代码会创建一个下拉菜单来让用户选择交易方式,选择后会自动调用 get_history() 函数来查询历史记录并在文本框中显示。注意,这个代码只是修改了查询历史记录的功能,如果要使用其他功能,可能需要进一步修改。