I’m trying to do a project with Tkinter, This program should be able to create new table in mysql. I want to create table-like entry boxes, I did it with a for loop, but I’m not sure how to obtain data that the user enters in that.
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')
con=mc.connect(host='localhost',user='root',passwd='hari@2468#')
cur=con.cursor()
def entry():
global e1,e2,e3,e4
p1=e1.get()
p2=e2.get()
p3=e3.get()
p4=e4.get()
cur.execute("Show databases")
d=cur.fetchall()
if (p1,) not in d:
cur.execute(f'create database {p1}')
cur.execute(f'use {p1}')
cur.execute("Show tables")
t=cur.fetchall()
if (p2,) in t:
messagebox.showinfo("Error",f"Table named {p2} already exists in provided database !! ")
else:
popup_win2=Toplevel()
popup_win2.wm_title("Column headers ")
cnamelst=[]
cconstlst=[]
choices=['Integer','Float','String','Date']
for i in range(int(p3)):
l=Label(popup_win2,text=f"Enter column {(i+1)} name : ")
l.grid(row=20+(5*i), column=20)
e=tkinter.Entry(popup_win2)
e.grid(row=20+(5*i), column=50)
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=80)
cconstlst.append(str(opt.get()))
cnamelst.append(str(e.get()))
(Button(popup_win2,text="OK", command=(print(cnamelst,cconstlst)))).grid(row=(int(p3)*10),column=50)
con.commit()
def register():
global e1,e2,e3,e4
(Label(ws,text="Enter the required details : ",font='arial 25 bold').pack)
(Label(ws,text="Enter a new database name : ",font='arial 15 bold').place(x=10,y=30))
e1=tkinter.Entry(ws)
e1.place(x=350,y=35)
(Label(ws,text="Enter a new table name : ",font='arial 15 bold').place(x=10,y=60))
e2=tkinter.Entry(ws)
e2.place(x=350,y=65)
(Label(ws,text="Enter a number of Columns : ",font='arial 15 bold').place(x=10,y=90))
e3=tkinter.Entry(ws)
e3.place(x=350,y=95)
(Label(ws,text="Enter a number of Rows : ",font='arial 15 bold').place(x=10,y=120))
e4=tkinter.Entry(ws)
e4.place(x=350,y=125)
(Button(ws,text="Confirm", command=entry)).place(x=200,y=150)
register()
ws.mainloop()
con.close()
In this, I need the column name to be appended in cnamelst and constraints that user chooses to be appended in cconstlst.
I’m not sure how to do so…
I’ll be thankful if someone can teach me how to do that.