繁体   English   中英

谁能告诉我如何使用相同的按钮解决 python gui tkinter 但在不同的 MySQL 表中单独工作

[英]Can anyone show me how to solve the python gui tkinter with same buttons but working separately in different MySQL tables

当我在 MySQL 数据库中只有一个学生表时,4 个按钮(插入、删除、更新、获取)工作得很好。 在我尝试在 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 命令? 谢谢!

我已经尝试解决我在代码中发现的大部分问题,但首先让我们关注您的主要问题。 理想的方法是使用一个像这样的组合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()

请注意,在这里,每个功能只需要 1 个按钮。

然后将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作为占位符的地方。 看看这里更好地理解这些

  • 我注意到您将函数命名为insert()get()等等。 这样的命名并不准确,因为某些 tkinter 小部件具有insert()delete()方法,因此稍后可能会导致 python 混淆。

  • 避免将变量命名为listid因为这些是内置函数,稍后会再次引起混淆。

  • 我已经导入了messagebox ,而我注意到你使用了Messagebox 我不确定tkinter.Messagebox存在,它可能会引发错误,如果不存在,则可以使用。

  • 您可以将更多 mysql 表名称添加到列表choices ,它会在组合Combobox上显示为选项。 在组合框上查看更多信息

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

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

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM