-1

The 4 buttons (insert, delete, update, get) worked perfectly when I only have a student table in the MySQL database. after I try to add the faculty and college in Python GUI and run, The 4 buttons won't work. I created the same 4 buttons under each table in the python GUI.

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()

How do I separate the 4 buttons for each table? Totally there are 12 buttons in my Python GUI (insert, delete, update, get)*3

What python command should I use? Thank you!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Zeddio
  • 1
  • 1
  • Please show us your code so we can better understand what you mean – nordmanden Sep 10 '20 at 11:52
  • I just edited, above is part of my python code. I take an example of the insert button. My question is how can I have 2 insert buttons work for different tables. one for student and another one for faculty. – Zeddio Sep 10 '20 at 12:28
  • You should have two different functions for two different buttons. Yes in this case you will need 4*3, 12 buttons, or you could give them options, and depending on their options, you could which table to show. Do let me know if you want a detailed answer – Delrius Euphoria Sep 10 '20 at 12:29
  • Yes, Can you please show me the example and code of how to do that. Thank you. – Zeddio Sep 10 '20 at 12:32
  • 1
    it's helpful! thank you so much! – Zeddio Sep 10 '20 at 16:06

1 Answers1

1

Ive tried to fix most of the issues I found with the code, but at first lets focus around your main problem. The ideal way would be to use would be a Combobox like:

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()

Note that here, you only need 1 button for each function.

Then define insertdb() as:

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()

So I hope you got a basic idea of whats happening. If not, Its like the user should choose an option at first, and then click on the button, only then the rest of the operation take place. Make necessary changes to the code, so that it fits to your needs.

Similarly, go on defining 3 other buttons and 3 functions, for your purpose.

What all have I changed?

  • You used concatenation for inserting data to the database and hence it is not a safe way and is exposed to sql injections. So i changed those to parametric substituions. Where you use %s as a placeholder. Take a look here for better understanding of these

  • I noticed that you named functions as insert(),get() and all. Naming as such is not accurate as some tkinter widgets has insert() and delete() methods, so it might cause a confusion to python later on.

  • Avoid naming variables as list, id as these are inbuilt functions and it will cause a confusion later on, again.

  • Ive imported messagebox, while i noticed you used Messagebox. Im not sure if tkinter.Messagebox exists and it might throw an error, if not, its fine to use.

  • You can add more mysql table names to the list choices and it will appear as options on the Combobox. Check out more on Combobox

  • Why did I say from tkinter import ttk? It is because Combobox is a ttk widget rather than a direct tkinter widget, which means it has a theme applied to it(looks modern).

I tried to explain this as simple as possible, hopefully you get a perfect idea on how to proceed. Do let me know if any errors, or doubts.

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46