import json
from tkinter import *
from tkinter import ttk
import pymysql
class discussClass():
def __init__(self, master:Frame):
self.huatiname = StringVar()
self.content=StringVar()
self.master = master
Label(master, text="这是讨论区管理界面").pack()
self.creatediscuss()
def creatediscuss(self):
self.master.configure(bg="#96CDCD")
# 创建 Treeview 组件
columns = ("huati", "neirong")
self.tree_view = ttk.Treeview(self.master, show='headings', columns=columns, style="Treeview")
self.tree_view.tag_configure("oddrow", background="#EAEAEA")
self.tree_view.tag_configure("evenrow", background="#FFFFFF")
self.tree_view.column("huati", width=80, anchor='center')
self.tree_view.column("neirong", width=80, anchor='center')
self.tree_view.heading('huati', text="话题")
self.tree_view.heading('neirong', text="内容")
self.tree_view.pack(pady=10, fill=BOTH, expand=True)
# 创建输入框
# 创建下拉框
options= json.loads(open('HUATI.json',mode='r',encoding='utf-8').read())
self.selected_option = StringVar()
self.dropdown = ttk.Combobox(self.master, textvariable=self.selected_option, values=options, width=20)
self.dropdown.pack(side="left", padx=5)
# 创建输入框
self.entry = Text(self.master, width=40,height=5)
self.entry.pack(side="left", padx=5)
# 创建新建讨论按钮
new_discussion_button = Button(self.master, text="发布",command=self.fabu)
new_discussion_button.pack(side=BOTTOM, padx=0, pady=(0, 10))
#Button.grid(row=0,column=0,columnspan=2,padx=10,pady=10)
#创建发布按钮
publish_button = Button(self.master, text="新建话题",command=self.add_discussion)
publish_button.pack(side=BOTTOM, padx=0, pady=(0, 10))
show_button = Button(self.master, text="刷新",command=self.show_discussion)
show_button.pack(side=BOTTOM, padx=0, pady=(0, 10))
for _ in map(self.tree_view.delete, self.tree_view.get_children("")):
pass
conn = pymysql.connect(
host='localhost',
user='root',
password='012977',
database='SHIXUN'
)
cursor = conn.cursor()
sql = "select * from Discuss"
cursor.execute(sql)
rest = cursor.fetchall()
index = 0
for i in rest:
print(i)
self.tree_view.insert("", index + 1, values=(i[0], i[1]))
def show_discussion(self):
self.dropdown['values']=['']
option = json.loads(open('HUATI.json', mode='r', encoding='utf-8').read())
self.dropdown['values']=option
for _ in map(self.tree_view.delete, self.tree_view.get_children("")):
pass
conn = pymysql.connect(
host='localhost',
user='root',
password='012977',
database='SHIXUN'
)
cursor = conn.cursor()
sql = "select * from Discuss"
cursor.execute(sql)
rest = cursor.fetchall()
index = 0
for i in rest:
print(i)
self.tree_view.insert("", index + 1, values=(i[0], i[1]))
def add_discussion(self):
root = Tk()
root.title("新建话题")
root.geometry("200x200")
Label(root, text="话题").pack()
entry = Entry(root, textvariable=self.huatiname)
entry.pack()
Button(root, text="提交", command=lambda: self.tijiao(entry.get(), root)).pack(pady=20)
def tijiao(self, new_topic, root):
with open("HUATI.json", "r", encoding="utf-8") as file:
data: list = json.load(file)
data.append(new_topic)
print(new_topic)
with open('HUATI.json', 'w') as file:
json.dump(data, file, indent=4)
# 关闭新建话题窗口
root.destroy()
def fabu(self):
a = self.selected_option.get()
b = self.entry.get("1.0", "end")
conn = pymysql.connect(
host='localhost',
user='root',
password='012977',
database='SHIXUN'
)
cursor = conn.cursor()
sql = "insert into Discuss values (%s,%s)"
cursor.executemany(sql,(a,b))
# 提交
conn.commit()
# 关闭
conn.close()
cursor.close()
报错
C:\Users\32756\PycharmProjects\TTK\.venv\Scripts\python.exe C:\Users\32756\PycharmProjects\TTK\SHIXUN\asd.py
('Python', 'dsasdasdh')
('houjianyu', '听说侯健羽是大傻逼')
('呵呵', '打打')
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\python39\Lib\tkinter\__init__.py", line 1967, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\32756\PycharmProjects\TTK\SHIXUN\discussClass.py", line 148, in fabu
cursor.executemany(sql,(a,b))
File "C:\Users\32756\PycharmProjects\TTK\.venv\Lib\site-packages\pymysql\cursors.py", line 182, in executemany
return self._do_execute_many(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\32756\PycharmProjects\TTK\.venv\Lib\site-packages\pymysql\cursors.py", line 205, in _do_execute_many
v = values % escape(next(args), conn)
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: not enough arguments for format string
进程已结束,退出代码为 0