有谁能告诉我如何使用相同的按钮,但在不同的MySQL表中分别工作来解决python gui问题吗

2024-10-02 02:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在MySQL数据库中只有一个student表时,4个按钮(insert、delete、update、get)工作得非常好。在我尝试在Python GUI中添加教员和学院并运行后,4个按钮将无法工作。 我在python GUI中的每个表下创建了相同的4个按钮

def insert():
    fid = e_fid.get()
    fname = e_fname.get();
    fsalary = e_fsalary.get();


    if(fid=="" or fsalary=="" or fname==""):
        MessageBox.showinfo("Insert status", "All fields are required")
    else:
        con = mysql.connect(host="localhost", user="root", password="", database="test0910")
        cursor = con.cursor()
        cursor.execute("insert into faculty values('"+ fid + "','"+ fname +"','"+ fsalary +"')")
        cursor.execute("commit");

        e_fid.delete(0, 'end')
        e_fname.delete(0, 'end')
        e_fsalary.delete(0, 'end')
        show()
        MessageBox.showinfo("Insert Status", "Inserted Successfully");
        con.close();


def insert():
    id = e_id.get()
    name = e_name.get();
    address = e_address.get();


    if(id=="" or name=="" or address==""):
        MessageBox.showinfo("Insert status", "All fields are required")
    else:
        con = mysql.connect(host="localhost", user="root", password="", database="test0910")
        cursor = con.cursor()
        cursor.execute("insert into student values('"+ id + "','"+ name +"','"+ address +"')")
        cursor.execute("commit");

        e_id.delete(0, 'end')
        e_name.delete(0, 'end')
        e_address.delete(0, 'end')
        show()
        MessageBox.showinfo("Insert Status", "Inserted Successfully");
        con.close();

root = Tk()
root.geometry("600x700")
root.title("Python+Tkinter+MySQL")

faculty = Label(root, text='Faculty', font=('bold', 15))
faculty.place(x=130, y=250);

fid = Label(root, text='Enter ID', font=('bold', 10))
fid.place(x=20, y=290);

fname = Label(root, text='Enter Name', font=('bold', 10))
fname.place(x=20, y=320);

fsalary = Label(root, text='Enter Salary', font=('bold', 10))
fsalary.place(x=20, y=350);

e_fid = Entry()
e_fid.place(x=150, y=290)

e_fname = Entry()
e_fname.place(x=150, y=320)

e_fsalary = Entry()
e_fsalary.place(x=150, y=350)

insert = Button(root, text="Insert", font=("italic", 10), bg="white", command=insert)
insert.place(x=40, y=390)

delete = Button(root, text="Delete", font=("italic", 10), bg="white", command=delete)
delete.place(x=100, y=390)

update = Button(root, text="Update", font=("italic", 10), bg="white", command=update)
update.place(x=160, y=390)

get = Button(root, text="Get", font=("italic", 10), bg="white", command=get)
get.place(x=225, y=390)

list = Listbox(root)
list.place(x=360, y=250)

student = Label(root, text='Student', font=('bold', 15))
student.place(x=130, y=470);

id = Label(root, text='Enter ID', font=('bold', 10))
id.place(x=20, y=510);

name = Label(root, text='Enter Name', font=('bold', 10))
name.place(x=20, y=540);

address = Label(root, text='Enter Address', font=('bold', 10))
address.place(x=20, y=570);

e_id = Entry()
e_id.place(x=150, y=510)

e_name = Entry()
e_name.place(x=150, y=540)

e_address = Entry()
e_address.place(x=150, y=570)



insert = Button(root, text="Insert", font=("italic", 10), bg="white", command=insert)
insert.place(x=40, y=610)

delete = Button(root, text="Delete", font=("italic", 10), bg="white", command=delete)
delete.place(x=100, y=610)

update = Button(root, text="Update", font=("italic", 10), bg="white", command=update)
update.place(x=160, y=610)

get = Button(root, text="Get", font=("italic", 10), bg="white", command=get)
get.place(x=225, y=610)

list = Listbox(root)
list.place(x=360, y=470)
show()

root.mainloop()

如何将每个表的4个按钮分开?在我的Python GUI中总共有12个按钮(插入、删除、更新、获取)*3

我应该使用什么python命令?谢谢大家!


Tags: textnameidgetaddressplacerootdelete
1条回答
网友
1楼 · 发布于 2024-10-02 02:29:17

我试图解决我在代码中发现的大多数问题,但首先让我们关注您的主要问题。理想的方法是使用Combobox类似:

from tkinter import ttk 
from tkinter import messagebox
.....

choices = ['Choose a table','faculty','student'] #list of options to be showed
combo = ttk.Combobox(root,values=choices,state='readonly') #declaring a combobox
combo.current(0) #setting the default value to first item in the list.
combo.pack()

buttoninsert = Button(root,text='Insert',command=insertdb)
buttoninsert.pack()

注意,在这里,每个函数只需要一个按钮

然后将insertdb()定义为:

def insertdb():
    if combo.get() == 'Choose a table':
        messagebox.showerror('Choose!','Choose an option!')
    
    elif combo.get() == 'faculty':
        fid = e_fid.get()
        fname = e_fname.get()
        fsalary = e_fsalary.get()

        if fid=="" or fsalary=="" or fname=="":
            messagebox.showinfo("Insert status", "All fields are required")
        else:
            con = mysql.connect(host="localhost", user="root", password="", database="test0910")
            cursor = con.cursor()
            cursor.execute("insert into faculty values(%s,%s,%s)",(fid,fname,fsalary))
            cursor.execute("commit")

            e_fid.delete(0, 'end')
            e_fname.delete(0, 'end')
            e_fsalary.delete(0, 'end')
            show()
            messageBox.showinfo("Insert Status", "Inserted Successfully");
            con.close()

    elif combo.get() == 'student':
        id = e_id.get()
        name = e_name.get();
        address = e_address.get();

        if id=="" or name=="" or address=="":
            messageBox.showinfo("Insert status", "All fields are required")
        
        else:
            con = mysql.connect(host="localhost", user="root", password="", database="test0910")
            cursor = con.cursor()
            cursor.execute("insert into student values(%s,%s,%s)",(id,name,address))
            cursor.execute("commit");

            e_id.delete(0, 'end')
            e_name.delete(0, 'end')
            e_address.delete(0, 'end')
            show()
            messageBox.showinfo("Insert Status", "Inserted Successfully");
            con.close()

所以我希望你对发生的事情有一个基本的了解。如果没有,用户应该首先选择一个选项,然后单击按钮,然后再执行其余的操作。对代码进行必要的更改,使其符合您的需要

同样,为了您的目的,继续定义其他3个按钮和3个函数

我改变了什么?

  • 您使用连接将数据插入数据库,因此这不是一种安全的方法,并且会暴露于sql注入。所以我把它们改成了参数取代基。其中使用%s作为占位符Take a look here for better understanding of these

  • 我注意到您将函数命名为insert()get()和all。这样的命名是不准确的,因为一些tkinter小部件有insert()delete()方法,所以它可能会在以后对python造成混淆

  • 避免将变量命名为listid,因为它们是内置函数,以后会再次引起混淆

  • 我导入了messagebox,而我注意到您使用了Messagebox。我不确定tkinter.Messagebox是否存在,它可能会抛出一个错误,如果不存在,就可以使用它

  • 您可以将更多mysql表名添加到列表choices,它将作为选项显示在ComboboxCheck out more on Combobox

  • 我为什么说from tkinter import ttk?这是因为Combobox是一个ttk小部件,而不是一个直接tkinter小部件,这意味着它应用了一个主题(看起来很现代)

我试着尽可能简单地解释这一点,希望你对如何进行有一个完美的想法。如果有任何错误或疑问,请务必告诉我

干杯

相关问题 更多 >

    热门问题