import mysql.connector
my_connect = mysql.connector.connect(
host="localhost",
user="userid",
passwd="password",
database="database_name"
)
####### end of connection ####
my_conn = my_connect.cursor()
We will use my_conn in our further script as the connection object to get our records.
my_conn = my_connect.cursor()
my_conn.execute("SELECT * FROM student limit 0,10")
i=0
for student in my_conn:
for j in range(len(student)):
print(student[j],end='')
i=i+1
print()# line break at the end of one row
This will print 10 rows of records from student table.
import mysql.connector
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x250")
my_connect = mysql.connector.connect(
host="localhost",
user="userid",
passwd="password",
database="database_name"
)
my_conn = my_connect.cursor()
####### end of connection ####
my_conn.execute("SELECT * FROM student limit 0,10")
i=0
for student in my_conn:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
i=i+1
my_w.mainloop()
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x250")
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/my_database")
####### end of connection ####
r_set=my_conn.execute("SELECT * FROM student limit 0,10")
i=0
for student in r_set:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
i=i+1
my_w.mainloop()
Using Label in place of Entry in above code.
for student in r_set:
for j in range(len(student)):
e = Label(my_w,width=10, text=student[j])
e.grid(row=i, column=j)
#e.insert(END, student[j])
i=i+1
We can add border to each cell by using borderwidth=2
, relief='ridge'
and align the text to left by using anchor='w'
.
e = Label(my_w,width=10, text=student[j],
borderwidth=2,relief='ridge', anchor="w")
e=Label(my_w,width=10,text='id',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=0)
e=Label(my_w,width=10,text='Name',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=1)
e=Label(my_w,width=10,text='Class',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=2)
e=Label(my_w,width=10,text='mark',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=3)
e=Label(my_w,width=10,text='Gender',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=4)
i=1
01-11-2020 | |
Thanks a lot...Loved it!! It works for me! |
21-11-2020 | |
Thanks a lot |
02-12-2020 | |
Hey, your code is working very well. Thank You so much. But there is one query. Every time the table is displayed in the Tk() window it goes to the top left corner. What is to be done if i want it somewhere in the middle? |
04-12-2020 | |
my_w.geometry('400x250+450+350') Here 450 and 350 are x and y position. You can change this value to position it at different locations. You can further improve it by reading user screen resolution and then apply it to dynamically position the window by changing these values. |
04-12-2020 | |
Don't use space while adding values to geometry(). This will not work my_w.geometry('400x250 + 450 + 350') |
02-07-2021 | |
thank you soo much !!!!!!!!!!!! |