Monday, April 27, 2020

entry with three form fields using tkinter in python

from tkinter import *
import pymysql


def create_table():
    conn=pymysql.connect(host="localhost",user="root",password="",db="sslabs")
    mycursor=conn.cursor()
    #mycursor.execute("CREATE TABLE student2 (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255),standard VARCHAR(255), sub VARCHAR(255) )")
    sql = "INSERT INTO student2 (name, standard,sub) VALUES (%s, %s,%s)"
 
    #print("First Name: %s\nstandard:%s\nsubject:%s" % (e1.get(),e2.get(),e3.get()))
    name=e1.get()
    standard=e2.get()
    sub=e3.get()
    val = (name, standard, sub)
    mycursor.execute(sql, val)
    conn.commit()
    print(mycursor.rowcount, "record inserted.")

def show_entry_fields():
    #print("First Name: %s\nstandard:%s\nsubject:%s" % (e1.get(),e2.get(),e3.get()))
    query = "SELECT * FROM student2"
    ## getting records from the table
    mycursor.execute(query)
    ## fetching all records from the 'cursor' object
    records = mycursor.fetchall()
    ## Showing the data
    for record in records:
        print(record)

master=Tk()
master.title("Basic Entry Widget")


Label(master , text="First Name").grid(row=0)
Label(master , text="standard").grid(row=1)
Label(master , text="sub").grid(row=2)

e1=Entry(master)
name=e1.grid(row=0,column=1)
e2=Entry(master)
standard = e2.grid(row=1,column=1)
e3=Entry(master)
sub = e3.grid(row=2,column=1)

Button(master,text='Quit', command=master.quit).grid(row=3,column=0,sticky=W,pady=4)
Button(master,text='Show', command=show_entry_fields).grid(row=3,column=1,sticky=W,pady=4)
Button(master,text='Insert', command=create_table).grid(row=3,column=2,sticky=W,pady=4)


mainloop()






 








python class topic video