Python Forum
[Tkinter] error on the 3rd function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] error on the 3rd function
#1
Dear Programmers
I have learned some programming in Python and wanted to apply and make a small app to save contacts.
I used three functions, function 1 will checks username and password and it works perfectly. function number 2 is to bring the main page after login is successful and function 3 has to send data to database, but it sends nothing. even I commented the rest of function and just tried to get whatever I was typing but no chance. I appreciate your solutions in this regards. Thanks in advance.
here is my code.
#-------------------this section is for login page----------------------#
from tkinter import *
import tkinter.messagebox
import mysql.connector
database = mysql.connector.connect(host="localhost", user="abc", passwd="abc", database="contactlist")
mycursor = database.cursor()
login = Tk()
login.geometry("550x400")

username = StringVar()
password = StringVar()
abc = StringVar()
name = StringVar()
lastname = StringVar()
mobile = StringVar()
mobile_2 = StringVar()
email = StringVar()
address = StringVar()
def search_func():
	pass

def exit_func():
	main_page.destroy()



def insert_func():
	db = mysql.connector.connect(host="localhost", user="abc", passwd="abc", database="contactlist")
	mycursor = db.cursor()
	n_name = name.get()
	n_lname = lastname.get()
	n_mobile = mobile.get()
	n_mobile_2 = mobile_2.get()
	n_email = email.get()
	n_address = address.get()
	print(str(n_name), n_lname, n_mobile, n_mobile_2, n_email, n_address)

"""
	if n_name != "" and n_lname != "" and n_mobile != "" and n_email !="" and n_address !="":
		sql = "INSERT INTO contact(name, lname, mobile, mobile_2, email, address) VALUES(%s, %s, %s, %s, %s, %s)"
		val = (n_name, n_lname, n_mobile, n_mobile_2, n_email, n_address)
		mycursor.execute(sql, val)
		db.commit()
		ent1.delete(0, END),ent2.delete(0, END),ent3.delete(0, END),ent4.delete(0, END),ent5.delete(0, END),ent6.delete(0, END)
		#------confirmation messege -------#
		confirmation_lab = Label(frame, text ="Record Has been added!")
		confirmation_lab.place(x=110, y=220)
		#confirmation_lab.place_forget() """
		
	#else:
		#print("error")

	#insert_page.mainloop()
		#tkinter.messagebox.showerror("Warning", "All marked field must not be empty!")


def insert_page_func():
	#db = mysql.connector.connect(host="localhost", user="abc", passwd="abc", database="contactlist")
	#mycursor = db.cursor()
	insert_page = Tk()
	insert_page.geometry("450x400")
	insert_page.title("Personel Information")
	frame = LabelFrame(insert_page, text=" Personnel Information ", padx=20, pady=30)
	frame.place(x=20, y=10, width=380, height=300)

	lab1 = Label(frame, text="Name:*")
	lab1.place(x=0, y=0)
	lab1 = Label(frame, text="Last Name:*")
	lab1.place(x=0, y=30)

	lab1 = Label(frame, text="Mobile:*")
	lab1.place(x=0, y=60)
	lab1 = Label(frame, text="Mobile-2")
	lab1.place(x=0, y=90)
	lab1 = Label(frame, text="Email:*")
	lab1.place(x=0, y=120)
	lab1 = Label(frame, text="Address:*")
	lab1.place(x=0, y=150)


	ent1 = Entry(frame, textvariable = name)
	ent1.place(x=100, y=0, width = 170)
	ent2 = Entry(frame, textvariable = lastname)
	ent2.place(x=100, y=30, width = 170)
	ent3 = Entry(frame, textvariable = mobile)
	ent3.place(x=100, y=60, width = 170)
	ent4 = Entry(frame, textvariable = mobile_2)
	ent4.place(x=100, y=90, width = 170)
	ent5 = Entry(frame, textvariable = email)
	ent5.place(x=100, y=120, width = 170)
	ent6 = Entry(frame, textvariable = address)
	ent6.place(x=100, y=150, width = 170)
	butt = Button(frame, text = "Save", bg = "red" , width=23, command = insert_func)
	butt.place(x=100, y=180)
	final_lab = Label(frame, text="Devloped by Nadim Softs 2020")
	final_lab.place(x=145, y=320)
	insert_page.mainloop()



def main_page_func():
	main_page = Tk()
	main_page.geometry("750x540")
	main_menu = Menu(main_page)
	main_page.configure(menu = main_menu)
	subMenu = Menu(main_menu)
	subMenu1 = Menu(main_menu)
	subMenu2 = Menu(main_menu)
	main_menu.add_cascade(label="File", menu=subMenu)
	main_menu.add_cascade(label="Tools", menu=subMenu1)
	main_menu.add_cascade(label="Extra", menu=subMenu2)	
	subMenu.add_command(label="Insert Record")
	subMenu.add_command(label="Delete Record")
	subMenu.add_command(label="Search")
	subMenu.add_command(label="Exit", command=exit_func)
	subMenu1.add_command(label="Export")
	subMenu1.add_command(label="Change password")
	subMenu2.add_command(label="Version")
	subMenu2.add_command(label="Help")
	insert_butt = Button(main_page, text="Insert", command=insert_page_func)
	insert_butt.place(x=10, y=5, width=50)
	delete_butt = Button(main_page, text="Delete")
	delete_butt.place(x=60, y=5, width=50)
	#---------test area section---------------#
	
	#search_entry = Entry(main_page, textvariable = search)
	#search_entry.place(x=20, y=40)
	ent1 = Entry(main_page, textvariable = abc)
	ent1.place(x=150, y=30)

	search_butt = Button(main_page, text="Search", command=search_func)
	search_butt.place(x=510, y=40, width=50)

	textarea = Text(main_page, height=30, width=70)
	textarea.place(x=20, y=100)
	main_page.mainloop()
	#------END of Main Page Function----------#





def login_func():
	n_username = username.get()
	n_password = password.get()
	if n_username == "admin" and n_password =="admin":
		login.destroy()
		main_page_func()
	else:
		tkinter.messagebox.showerror("Warning", "Please enter your correct credentials!")

frame = LabelFrame(login, text=" Personel Credentials ", padx=20, pady=30)
frame.place(x=40, y=10, width=440, height=340)
lab1 = Label(frame, text="Username:")
lab1.place(x=30, y = 30)
lab2 = Label(frame, text="Password:")
lab2.place(x=30, y = 60)
ent1 = Entry(frame, textvariable = username)
ent1.place(x=150, y=30)
ent2 = Entry(frame, textvariable = password)
ent2.place(x=150, y=60)
butt = Button(frame, text="Login",bg="red", command=login_func)
butt.place(x=150, y=90, width =120)




login.mainloop()
Reply
#2
What is your question? I think you are talking about "insert_func()" since it has a lot of code commented out.

What do you mean by this "even I commented the rest of function and just tried to get whatever I was typing but no chance" Is nothing printed at all, or is it printing the wrong thing? If wrong, how is it wrong?

I was able to run your program and look at the StringVar's you reference in "insert_func()". They are all blank (''). Then I looked for where the variables are bound to controls, and they aren't. You properly bind variables to controls in your login window (while kind of), but you do not bind any variables to the controls in you insert window.
Reply
#3
short answer convert your script to a class long answer the functions you create are private and the variables are too. Easiest way is to create a class, use oop, use the self. prefix, and convert what you have- I think you may be better off getting your information directly from the entry widgets then using a stringVar, they are setup to constantly change, example in a label to show different values from a slider or a constantly changing score board
Reply
#4
The problem doesn't have much to do with scope. The problem is the program creates a bunch of tk Variables and then doesn't bind them to controls. I don't do a lot of programming in tk anymore, but Variables are obviously an attempt to separate your logic (what the program does) from your view (how your program looks). I would stick with using Variables because they make it easier to write more complex programs. May as well learn how to use them in a simple program like this.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020