I’m developing it a program to create a table in MySQL , But now in the last part where I should be getting the values in the table, I need to add a label containing the column name.
I tried it, but ended up unsuccessful messing the layout of the table with each try…
Please can you help me by fixing that part for me
from tkinter import *
from tkinter import messagebox
import tkinter
import random
import mysql.connector as mc
ws = Tk()
ws.title('My SQL Table Creator')
ws.geometry('1920x1080')
ws.config(bg='#82E0AA')
def pkey():
global cur, liname,tbg,pop_win,opti
pop_win=Toplevel()
pop_win.geometry("640x420")
opti=tkinter.StringVar(pop_win)
opti.set("Select primary key : ")
pk=tkinter.OptionMenu(pop_win,opti ,*liname)
pk.grid(row=5,column=10)
(Button(pop_win,text=" Set primary key ", command=build_table)).grid(row=10,column=10)
def create():
global cur, tbg,le
for i in le:
v=()
for j in i:
v+=(eval(j.get()),)
q=f"insert into {tbg} values {v}"
cur.execute(q)
con.commit()
messagebox.showinfo("Success","Table created succesfully !! ")
ws.destroy()
def build_table():
global cur, rng,cng,le,liname,pop_win,opti
pn=opti.get()
q=f"alter table {tbg} add primary key ({pn})"
cur.execute(q)
pop_win.destroy()
popup_win=Toplevel()
popup_win.geometry('1920x1080')
for k in range(len(liname)):
t=liname[k]
lb=Label(popup_win,text=f" {t} ")
lb.grid(row=1,column=k*10)
le=[]
for i in range(int(rng)):
lb.grid(row=i,column=1)
se=[]
for j in range(int(cng)):
e=Entry(popup_win)
e.grid(row=(i+1)*10,column=(j+1)*10)
se.append(e)
le.append(se)
Button(popup_win, text=" Create table ", command=create).grid(row=(int(rng)+5)*10,column=(int(cng)+5)*10)
def fetch():
global cur, licon, liname,q,tbg,popup_win2
licon=[]
liname=[]
for i in cconstlst:
licon.append(i.get())
for j in cnamelst:
liname.append(j.get())
q='create table '
if len(liname)==len(licon):
for i in range(len(licon)):
if licon[i] == 'String':
licon[i]='Varchar(225)'
qm=str(tbg)+' ('
for a in range(len(liname)):
qm+=liname[a]+' '+licon[a]+', '
q=q+qm[:-2]+')'
cur.execute(q)
popup_win2.destroy()
pkey()
def entry():
global con,cur,dbe,tbe,cne,rne,cconstlst,cnamelst,dbg,tbg,cng,rng,popup_win2,une,pwe
ung=une.get()
pwg=pwe.get()
dbg=dbe.get()
tbg=tbe.get()
cng=cne.get()
rng=rne.get()
con=mc.connect(host='localhost', username=ung, password=pwg)
cur=con.cursor()
cur.execute("Show databases")
d=cur.fetchall()
if (dbg,) not in d:
cur.execute(f'create database {dbg}')
cur.execute(f'use {dbg}')
cur.execute("Show tables")
t=cur.fetchall()
if (tbg,) in t:
messagebox.showinfo("Error",f"Table named {tbg} already exists in provided database !! ")
else:
popup_win2=Toplevel()
popup_win2.wm_title("Column headers ")
popup_win2.geometry("740x520")
popup_win2.config(bg='#A569BD')
cnamelst=[]
cconstlst=[]
choices=['Integer','Float','String','Date']
for i in range(int(cng)):
l=Label(popup_win2,text=f"Enter column {(i+1)} name : ")
l.grid(row=20+(5*i), column=50)
e=tkinter.Entry(popup_win2)
e.grid(row=20+(5*i), column=150)
opt=tkinter.StringVar(popup_win2)
opt.set("Select data type constraints : ")
c=tkinter.OptionMenu(popup_win2,opt ,*choices)
c.grid(row=20+(5*i), column=250)
cconstlst.append(opt)
cnamelst.append(e)
(Button(popup_win2,text="OK", command=(fetch))).grid(row=(int(cng)+5*10),column=200)
con.commit()
def register():
global cur, dbe,tbe,cne,rne,une,pwe
(Label(ws,text="Enter the required details : ",font='Magneto 30 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=30))
(Label(ws,text="Enter your mysql user_name : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=120))
une=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
une.place(x=700,y=120)
(Label(ws,text="Enter your mysql password : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=160))
pwe=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
pwe.place(x=700,y=160)
(Label(ws,text="Enter a new database name : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=200))
dbe=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
dbe.place(x=700,y=200)
(Label(ws,text="Enter a new table name : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=240))
tbe=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
tbe.place(x=700,y=240)
(Label(ws,text="Enter a number of Columns : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=280))
cne=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
cne.place(x=700,y=280)
(Label(ws,text="Enter a number of Rows : ",font='Castellar 12 bold',highlightbackground="#141414",highlightthickness=2).place(x=300,y=320))
rne=tkinter.Entry(ws,highlightbackground="#141414",highlightthickness=2)
rne.place(x=700,y=320)
(Button(ws,highlightbackground="#141414",highlightthickness=2,text="Confirm", command=entry)).place(x=600,y=380)
register()
con.commit()
ws.mainloop()
con.close()