Empty entries filled in MySQL tables (Python + Tkinter)
Having a problem of data entry in MySQL. At the time of entry it don't take any value from me or
user. And one more thing I want to add multiple pages. But how can I make button to submit the value and direct to page 2. please if anyone know then please me.
Thank you
Here he the table which is completely empty.
from tkinter import *
import mysql.connector
from PIL import Image,ImageTk
m=Tk()
button_bg='#FF9200'
button_fg='#fff'
active_button_fg='#FF9200'
active_button_bg='#fff'
def tkinter_setup():
m.geometry('1024x720')
def database_connectivity():
FullName=StringVar()
CollegeName=StringVar()
Email = StringVar()
Password=IntVar()
CGPA= IntVar()
fullName=FullName.get()
collegeName=CollegeName.get()
email=Email.get()
password=Password.get()
cgpa=CGPA.get()
mydb = mysql.connector.connect(host="localhost", user="root", passwd="ashu12",database='mySchool')
cursor=mydb.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS Student (FullName TEXT,CollegeName TEXT,Email TEXT,Password INT,CGPA INT)')
cursor.execute('INSERT INTO Student (FullName,CollegeName,Email,Password,CGPA) VALUES(%s,%s,%s,%s,%s)',(fullName,collegeName,email,password,cgpa))
mydb.commit()
def page2():
entry7 = Entry(m,width=70)
entry7.place(x=0,y=30)
entry7.insert(0,'Full Name')
m.mainloop()
def page1():
Image_open=Image.open("1.png")
image=ImageTk.PhotoImage(Image_open)
logo=Label(m,image=image)
logo.place(x=0,y=0,bordermode="outside")
entry1 = Entry(m,textvar='FullName',font="Ubuntu")
entry1.place(height=45,width=397,x=145,y=154)
entry1 = Entry(m,width=42,textvar='Collegename',font="Ubuntu")
entry1.place(height=45,width=397,x=145,y=210)
entry1 = Entry(m,width=42,textvar='Email',font="Ubuntu")
entry1.place(height=45,width=397,x=145,y=266)
entry1 = Entry(m,width=42,textvar='Password',font="Ubuntu")
entry1.place(height=45,width=397,x=145,y=322)
entry1 = Entry(m,width=42,textvar='CGPA',font="Ubuntu")
entry1.place(height=45,width=397,x=145,y=377)
button = Button(m,text='Registration' , bg=button_bg,foreground=button_fg,activebackground=active_button_bg,activeforeground=active_button_fg,command=page2 )
button.place(height=47, width=399 ,x=144,y=476)
m.mainloop()
tkinter_setup()
database_connectivity()
page1()
page2()
Related
I'm a rookie in Python. I have tested Python for 5 days. And managed to create my app and have everything set up in GUI and for the database connection . I'm using Custom TKinter
But can't get this Right
class CustomOptionMenu(customtkinter.CTkOptionMenu):
def __init__(self, master, value, *options):
print("CustomOptionMenu dropdown clicked:")
self.var = customtkinter.StringVar(value=value)
customtkinter.CTkOptionMenu.__init__(self, master, self.var, *options)
self.configure(font=("calibri",(10)), bg='white', width=12)
# Option Menu for tablespaces
label_tablespace = customtkinter.CTkLabel(Menu.tab("Create Datafile"),text="Tablespace:")
label_tablespace.pack(pady=5)
tablespaces = get_all_tablespaces()
tablespace_value = customtkinter.StringVar(value="Select Table Space")
options = ["Select Table Space"] + tablespaces
option_menu = CustomOptionMenu(Menu.tab("Create Datafile"), tablespace_value, *options)
option_menu.pack(padx=20, pady=10)
def get_all_tablespaces():
global conn
cursor = conn.cursor()
table_name = "dba_tablespaces"
query = "SELECT tablespace_name FROM " + table_name
cursor.execute(query)
tablespaces = cursor.fetchall()
if not tablespaces:
messagebox.showerror("Error", "No tablespaces found")
return
tablespace_names = [tablespace[0] for tablespace in tablespaces]
return tablespace_names
My mind is going crazy I don't know where I messed up. Can some one help me. So I can learn something from my mistake.
Old Tkinter seems working, but I'm not getting it right with the new custom TKinter.
I have a dropdown menu that gets it's options from a sqlite database. I have a function that adds to the dropdown, but I'm struggling to make a delete function. I want the option menu selection to get deleted if you press delete. Could someone please help me with this? Thanks.
from tkinter import *
import os
import sqlite3
# init window
window=Tk()
window.title("Scheduling Assistant")
window.geometry("400x300")
addname1 = Label(text="Add Last Name:")
addname = Entry()
addquality1 = Label(text="Add Quality (A,B, or C):")
addquality = Entry()
addname1.pack()
addname.pack()
addquality1.pack()
addquality.pack()
# create table
conn = sqlite3.connect("dentist0.db")
c = conn.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS densist(
last_name text,
class text)""")
clicked = StringVar()
drpdwn = OptionMenu(window, clicked, [])
def update_menu():
c.execute("SELECT last_name FROM densist")
all = c.fetchall()
print(all)
drpdwn['menu'].delete(0, 'end')
for choice in all:
drpdwn['menu'].add_command(label=choice)
def click1():
lstnme = addname.get()
quality = addquality.get()
sql = "INSERT INTO densist VALUES (?,?)"
c.execute(sql, [lstnme, quality])
conn.commit()
update_menu()
def click2():
sql2="DELETE FROM densist WHERE last_name=last_name"
c.execute(sql2)
conn.commit()
submit = Button(text='Add',command=click1)
submit2=Button(text='Delete', command=click2)
submit.pack()
submit2.pack()
drpdwn.pack()
update_menu()
window.mainloop()
I have Python program connected to SQLite 3 database with Tkinter on the frontend. My database table (subjectlist) consists of three columns: [id (unique interger), subject (text), serial (unique integer)]. Here is my program:
import sqlite3
from tkinter import *
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT COUNT() FROM subjectlist')
number = (c.fetchone()[0])
c.execute('SELECT * FROM subjectlist ORDER BY serial')
data = c.fetchall()
c.close
conn.close()
root = Tk()
listbox = Listbox(root)
listbox.pack()
for i in range(number):
listbox.insert(END, data[i][1])
def get_serial():
print(listbox.get(listbox.curselection()))
btn = Button(root, text="Show serial", command=lambda: get_serial())
btn.pack()
mainloop()
Currently at runtime when I click item on listbox (witch basically shows all subject column values) and then press Tkinter button I get the subject I clicked. Instead I want to get serial corresponding to the subject. Notice that subject column may have same value on two or more different rows. How would I go about achieving this?
Here is just one more example; If I have this table:
I want the GUI to show first all of the subjects in the listbox. Then I click "Cats" (on the the id row 3) and then I click the button, I want the program to print me serial 4.
Well, this should also be easy. But since there is a database in your code that I can't currently test this with it, I made some assumptions. Try this out:
def get_letter():
conn = sqlite3.connect('database.db')
c = conn.cursor()
ids = listbox.curselection()[0]+1 # Getting the index number starting from 1
c.execute('SELECT * FROM subjectlist WHERE `id`=?;',(ids,)) # Searching the database for items with selected items index
rows = c.fetchall()
print(rows[0]) # Print the first and hopefully the only item in the list
conn.close() # Close the connection
This should print the rows which corresponds to the id, and since usually id is a unique number, it will only print out one row. I'm also assuming that you have both the database and the listbox in the same order or this might not work.
You can use the index of the item inside listbox to get the serial from data:
import sqlite3
from tkinter import *
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('SELECT * FROM subjectlist ORDER BY serial')
data = c.fetchall()
c.close()
conn.close()
root = Tk()
listbox = Listbox(root)
listbox.pack()
for rec in data:
listbox.insert(END, rec[1])
def get_serial():
selected = listbox.curselection()
if selected:
idx = selected[0]
print(data[idx][2]) # print serial of selected item
btn = Button(root, text="Show serial", command=get_serial)
btn.pack()
mainloop()
Note that you should make sure data and listbox are synchronised.
I want to use Tkinter to create a search form where the user can input the name they want to see from the SQLite3 database. A database named New_Assignment has all the details about the person. But I am confused about how to connect the Tkinter to the database and use the name to search? This is what I have got so far.
import sqlite3
conn = sqlite3.connect('new_assignment.db')
c = conn.cursor()
from tkinter import *
top = Tk()
top.title('Search form')
person_name = Entry()
person_name.pack(side = RIGHT, expand = True)
mainloop()
To get the text from the Entry widget, use the get() method (it returns the string in the textbox).
name_box = Entry()
name_box.pack(side=RIGHT, expand=True)
person_name = name_box.get()
However, you will need to add a button as #abarnert says in the comments. Attach this function to it:
def get_name():
global person_name
person_name = name_box.get()
data = c.fetchall()
print(data)
for row in data:
searchlist.append(row)
var1=str(person_name)
read_from_db(var1)
(Of couse, modify read_from_db() to take a variable like so:
def read_from_db(var):
curs.execute("SELECT *" + " FROM personal WHERE Name LIKE (?)", ('%'+var+'%',))
)
An example of the code for a button might look like this:
button = Button(top, text="Display text", command=get_name)
Place it all together:
import sqlite3
conn = sqlite3.connect('new_assignment.db')
c = conn.cursor()
from tkinter import *
top = Tk()
top.title('Search form')
name_box = Entry()
name_box.pack(side = RIGHT, expand = True)
def get_name():
global person_name
person_name = name_box.get()
data = c.fetchall()
print(data)
for row in data:
searchlist.append(row)
var1=str(person_name)
read_from_db(var1)
def read_from_db(var):
curs.execute("SELECT *" + " FROM personal WHERE Name LIKE (?)", ('%'+var+'%',))
person_name = ""
button = Button(top, text="Display text", command=get_name)
button.pack()
mainloop()
I want to querying data from sqlite3 db from python and populate it in a Listbox but if am doing the search i have to provide the full name like Harvard University before the records can be inserted into the Listbox.
I want to query record like Harvard University by providing only Harvard then it will output all records with Havard content in it for me because if i don't provide full name it will not populate the listbox with any record.
Your suggestions are welcome to achieve this.
import tkinter as tk
import sqlite3
conn = sqlite3.connect("STATS.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS institution(id INTEGER PRIMARY KEY,
name TEXT)")
conn.commit()
conn.close()
def query_record():
data1 = e1_search.get()
conn = sqlite3.connect("STATS.db")
cur = conn.cursor()
cur.execute("SELECT * FROM institution WHERE name=?", (data1,))
row = cur.fetchall()
for n in row:
List.insert(tk.END, n)
print(n)
conn.close()
root = tk.Tk()
root.geometry("300x300")
List = tk.Listbox(root, width=100)
List.pack()
e1_search = tk.StringVar()
e1 = tk.Entry(root, textvariable=e1_search)
e1.pack()
b = tk.Button(text="Search", command=query_record)
b.pack(side=tk.BOTTOM)
root.mainloop()
I think you may have to modify the query.
Since "SELECT * FROM institution WHERE name=?", (data1,)
will exactly match the input string you can try with,
"SELECT * FROM institution WHERE name like ?", (data1+'%',).
Hope this works!