Related
im working on this tkinter project im close to finishing but i cant seem to find a way to change the button color when i hover on it so can you help here is my code
import tkinter as tk
window = tk.Tk()
img = tk.PhotoImage(file='C:\\Users\\laithmaree\\PycharmProjects\\create_apps_with_python\\brainicon.ico.png')
window.title("Quiz Game")
# i created an icon
# i made a title
window.geometry("800x600")
window.resizable(width=False, height=False)
window.iconphoto(False, img)
label1 = tk.Label(window, text='Quiz App', font=("Arial Bold", 25))
label1.pack()
txtbox = tk.Entry(window, width=50)
def playbuttonclicked():
label1.destroy()
playbtn.destroy()
quitbtn.destroy()
label2 = tk.Label(window, text='What is the short form of computer science', font=("Arial Bold", 25))
label2.pack()
txtbox.place(x=250, y=200, height=40)
def chkanswer():
useranswer = txtbox.get() # Get contents from Entry
if useranswer == 'cs':
lblcorrect = tk.Label(window, text='correct')
lblcorrect.pack()
def delete():
lblcorrect.destroy()
lblcorrect.after(1000, delete)
else:
lblwrong = tk.Label(window, text='Try Again')
lblwrong.pack()
def deletefunction():
lblwrong.destroy()
lblwrong.after(1000, deletefunction)
submitbtn = tk.Button(window, text='Submit', font=('Arial Bold', 30), command=chkanswer, bg='red')
submitbtn.place(x=305, y=400)
playbtn = tk.Button(window, text='Play', font=("Arial Bold", 90), bg='red', command=playbuttonclicked)
playbtn.place(x=10, y=200)
def quitbuttonclicked():
window.destroy()
quitbtn = tk.Button(window, text='Quit', font=("Arial Bold", 90), bg='red', command=quitbuttonclicked)
quitbtn.place(x=400, y=200)
window.mainloop()
the buttons are submitbtn,playbtn,quitbtn i want the hover over buttons to be black there already red i just want them to be black when i hover over them thx
Bind each of the buttons to entering and leaving events (when mouse enters and leaves the widget) and based on which one is triggered, change the background color of the button:
btn.bind('<Enter>', lambda e: e.widget.config(bg='black'))
btn.bind('<Leave>', lambda e: e.widget.config(bg='red'))
Im trying to enter to the menu window but whenever I do so I get the cant invoke frame command. I am not sure if there is an error regarding with my .mainloop() function
Here is my login code
class Login:
global user
global pwd
def __init__(self,root):
self.root = root
self.root.title("Scheduling Management System")
self.root.geometry("1350x768+0+0")
self.root.resizable(False, False)
self.txt_user = StringVar()
self.txt_pass = StringVar()
self.bg = ImageTk.PhotoImage(file="Images/bgimage.jpg")
bg = Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)
framelogin = Frame(self.root, bg="white")
framelogin.place(x=450, y=100, height=500, width=700)
registerbutton = Button(framelogin, text="Sign Up", command=self.register, bg="white", fg="orange",
font=("trebuchet ms", 12)).place(x=510, y=305)
loginbutton = Button(framelogin, text="Login", command=self.login, fg="white", bg="orange",
font=("sans serif ms", 20)).place(x=90, y=350, width="100", height="40")
def login(self):
global con
user = self.txt_user.get().strip()
pwd = self.txt_pass.get().strip()
if user == "" or pwd == "":
messagebox.showerror("Error", "Please fill up all fields!")
else:
try :
con=pymysql.connect(host="localhost",user="root",password="",database="employee")
cur=con.cursor()
cur.execute("select 1 from employeelist where username=%s and password=%s", (user,pwd))
if cur.rowcount == 1:
messagebox.showinfo("Success", "Login Successful", parent=self.root)
self.menu()
else:
messagebox.showerror("Error", "Wrong Username or Password. Please try again!")
except Exception as ex:
messagebox.showerror("Error",f"Error due to: {str(ex)}",parent=self.root)
finally:
con.close()
def register(self):
self.root.destroy()
register.RegisterForm()
def menu(self):
self.root.destroy()
menu.MenuForm()
def LoginForm():
win = Tk()
obj = Login(win)
if __name__ == "__main__":
root = Tk()
obj = Login(root)
root.mainloop()
Here is my menu code
from tkinter import *
from tkinter import ttk, messagebox
import PIL
from PIL import ImageTk
from PIL import Image
import login
class Menu:
def __init__(self,root):
self.root = root
self.root.title("Main Menu")
self.root.geometry("1350x768+0+0")
self.bg = ImageTk.PhotoImage(file="Images/bgimage.jpg")
bg = Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)
framemenu = Frame(self.root, bg="white")
framemenu.place(x=450, y=100, height=500, width=700)
welcometitle = Label(framemenu, text="Welcome " + login.Login.user, font=("Arial", 30, "bold"), fg="orange", bg="white").place(x=470,
y=120)
def MenuForm():
win = Tk()
obj = Menu(win)
if __name__ == "__main__":
root = Tk()
obj = Menu(root)
root.mainloop()
I cant seem to identify the issue as Im trying to redirect the users once it has login to the menu page
Im trying to close my login wondow and open my register window once I click the sign up button but it doesn't destroy the login window
Here is my login code
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import PIL
from PIL import ImageTk
from PIL import Image
import register
import pymysql
class Login:
def __init__(self,root):
self.root = root
self.root.title("Scheduling Management System")
self.root.geometry("1350x768+0+0")
self.root.resizable(False, False)
self.txt_user = StringVar()
self.txt_pass = StringVar()
self.bg = ImageTk.PhotoImage(file="Images/bgimage.jpg")
bg = Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)
framelogin = Frame(self.root, bg="white")
framelogin.place(x=450, y=100, height=500, width=700)
title = Label(framelogin, text="Login Here", font=("Arial", 30, "bold"), fg="orange", bg="white").place(x=90,
y=30)
nexttitle = Label(framelogin, text="Scheduling Staff System", font=("Times New Roman", 18, "bold"), fg="orange",
bg="white").place(x=90, y=100)
userlabel = Label(framelogin, text="Username", font=("Arial", 15, "bold"), fg="gray", bg="white").place(x=90,
y=140)
self.txt_user = Entry(framelogin, textvariable=self.txt_user, font=("times new roman", 15), bg="lightgray")
self.txt_user.place(x=90, y=170, width=350, height=35)
passlabel = Label(framelogin, text="Password", font=("Arial", 15, "bold"), fg="gray", bg="white").place(x=90,
y=210)
self.txt_pass = Entry(framelogin, textvariable=self.txt_pass, font=("times new roman", 15), show="*",
bg="lightgray")
self.txt_pass.place(x=90, y=240, width=350, height=35)
forget = Button(framelogin, text="Forgot Password", bg="white", fg="orange", font=("trebuchet ms", 12)).place(
x=90, y=305)
reglabel = Label(framelogin, text="Don't Have an Account?", font=("trebuchet ms", 12, "bold"), fg="orange",
bg="white").place(x=320, y=310)
registerbutton = Button(framelogin, text="Sign Up", command=self.register, bg="white", fg="orange",
font=("trebuchet ms", 12)).place(x=510, y=305)
loginbutton = Button(framelogin, text="Login", command=self.login, fg="white", bg="orange",
font=("sans serif ms", 20)).place(x=90, y=350, width="100", height="40")
def login(self):
if self.txt_user.get() == "" or self.txt_pass.get() == "":
messagebox.showerror("Error", "Please fill up all fields!")
def register(self):
register.RegisterForm()
if __name__ == "__main__":
root = Tk()
obj = Login(root)
root.mainloop()
Here is my register code
from tkinter import *
from tkinter import ttk, messagebox
import PIL
import pymysql
from PIL import ImageTk
from PIL import Image
import login
class Register:
def __init__(self, root):
self.root = root
...
btn = Button(frame1,image=self.btn, bd=0, command = self.registerdata,cursor = "hand2").place(x=50, y = 420)
def registerdata(self):
if self.text_fname.get()=="" or self.text_lname.get()=="" or self.text_contact.get()=="" or self.text_email.get()=="" or self.cmbquestion.get()=="Select" or self.text_pwd.get()=="" or self.text_cfmpwd.get()=="":
messagebox.showerror("Error","All fields are required!",parent=self.root)
elif self.text_pwd.get()!=self.text_cfmpwd.get():
messagebox.showerror("Error","Passwords must be the same!",parent=self.root)
else:
try:
con=pymysql.connect(host="localhost",user="root",password="",database="employee")
cur=con.cursor()
cur.execute("select * from employeelist where email=%s", self.text_email.get())
row=cur.fetchone()
print(row)
if row!=None:
messagebox.showerror("Error","User Already Exists. Please Register With a New Email",parent=self.root)
else:
cur.execute("insert into employeelist (fname,lname,contact,email,question,answer,password) values(%s,%s,%s,%s,%s,%s,%s)",
(self.text_fname.get(),self.text_lname.get(),self.text_contact.get(),self.text_email.get(),self.cmbquestion.get(),self.text_answer.get(),self.text_pwd.get()))
con.commit() #do changes to database
con.close()
messagebox.showinfo("Success","Registration Successful",parent=self.root)
except Exception as ex:
messagebox.showerror("Error",f"Error due to: {str(ex)}",parent=self.root)
l = login
l.Login(root)
def RegisterForm():
win = Toplevel()
obj = Register(win)
if __name__ == "__main__":
root = Tk()
obj = Register(root)
root.mainloop()
Any ideas on how to do so as currently I'm only able to see both windows instead of destroying login window and opening register window?
Firstly add self.root.destroy() to the register method in the login file.
This will destroy the login Tk instance.
To avoid the extra window when the register window opens, change win = Toplevel() in RegisterForm to win = Tk(). Toplevel needs a Tk window so it made an extra window, so we need to create a Tk instance instead.
I have written my code so that when the button "SignIn" is pressed it calls the function "Login". However, every time I run the code and press the button, the error message "_tkinter.TclError: image "pyimage2" doesn't exist" is displayed and I cannot seem to find a solution which fixes my code.
import tkinter
def Login():
window = tkinter.Tk()
window.title("Eat Well")
window.geometry("295x400")
UsernameLbl = tkinter.Label(window, text = "Username", fg= "white", bg= "black")
Utext = tkinter.Entry(window)
PasswordLbl = tkinter.Label(window, text = "Password", fg = "white", bg= "black")
Ptext = tkinter.Entry(window, show="*")
Login = tkinter.Button(window, text = "Login", fg = "black", bg = "honeydew", command = window.destroy )
window.configure(background= "#008bb5")
Photo = tkinter.PhotoImage(file = "Eating.gif")
w = tkinter.Label(window, image = Photo)
w.pack()
UsernameLbl.pack()
Utext.pack()
PasswordLbl.pack()
Ptext.pack()
Login.pack()
window.mainloop()
def Mainscreen():
window = tkinter.Tk()
window.title("Eat Well")
window.geometry("295x400")
Question = tkinter.Label(window, text = "Would you like to create an account or login?", fg = "black", bg = "white")
Create = tkinter.Button(window, text = "Create an account", fg = "white", bg = "black")
SignIn = tkinter.Button(window, text = "Login", fg = "white", bg = "black", command = Login)
Quit = tkinter.Button(window, text = "Quit", fg = "white", bg = "black", command = window.destroy)
window.configure(background = "#008bb5")
Photo = tkinter.PhotoImage(file = "Eating.gif")
w = tkinter.Label(window, image = Photo)
w.pack()
Question.pack()
Create.pack()
SignIn.pack()
Quit.pack()
window.mainloop()
Mainscreen()
When the SignIn button is pressed, the MainScreen should be destroyed and the Login screen should be opened. However, currently whenever the login button is pressed on the main screen, the MainScreen remains open and the Login screen is displayed as a blank screen.
This should work. Notice the use of
`tkinter.Toplevel()
and Image.open. This is because the button that calls the function is itself sitting in an active window.
import tkinter
from PIL import Image, ImageTk
def Login():
window = tkinter.Toplevel()
window.title("Eat Well")
window.geometry("295x400")
UsernameLbl = tkinter.Label(window, text = "Username", fg= "white", bg= "black")
Utext = tkinter.Entry(window)
PasswordLbl = tkinter.Label(window, text = "Password", fg = "white", bg= "black")
Ptext = tkinter.Entry(window, show="*")
Login = tkinter.Button(window, text = "Login", fg = "black", bg = "honeydew", command = window.destroy )
window.configure(background= "#008bb5")
im = Image.open("Eating.gif")
Photo = ImageTk.PhotoImage(im)
w = tkinter.Label(window)
w.pack()
UsernameLbl.pack()
Utext.pack()
PasswordLbl.pack()
Ptext.pack()
Login.pack()
window.mainloop()
def Mainscreen():
window = tkinter.Tk()
window.title("Eat Well")
window.geometry("295x400")
Question = tkinter.Label(window, text = "Would you like to create an account or login?", fg = "black", bg = "white")
Create = tkinter.Button(window, text = "Create an account", fg = "white", bg = "black")
SignIn = tkinter.Button(window, text = "Login", fg = "white", bg = "black", command = Login)
Quit = tkinter.Button(window, text = "Quit", fg = "white", bg = "black", command = window.destroy)
window.configure(background = "#008bb5")
im = Image.open("Eating.gif")
Photo = ImageTk.PhotoImage(im)
w = tkinter.Label(window)
w.pack()
Question.pack()
Create.pack()
SignIn.pack()
Quit.pack()
window.mainloop()
Okay so the problem is you are trying to run two instances of Tk() simultaneously which you should not. Reasons are described here and here also
Instead of window = tkinter.Tk() in your Login() you can use window = tkinter.Toplevel() to solve the problem like following:
import tkinter
def Login():
# window = tkinter.Tk()
window = tkinter.Toplevel()
window.title("Eat Well")
window.geometry("295x400")
user_name_label = tkinter.Label(window, text="Username", fg="white", bg="black")
user_name_text = tkinter.Entry(window)
password_label = tkinter.Label(window, text="Password", fg="white", bg="black")
password_text = tkinter.Entry(window, show="*")
login = tkinter.Button(window, text="Login", fg="black", bg="honeydew", command=window.destroy)
window.configure(background="#008bb5")
photo = tkinter.PhotoImage(file="Eating.gif")
w = tkinter.Label(window, image=photo)
w.pack()
user_name_label.pack()
user_name_text.pack()
password_label.pack()
password_text.pack()
login.pack()
window.mainloop()
def Mainscreen():
window = tkinter.Tk()
window.title("Eat Well")
window.geometry("295x400")
question = tkinter.Label(window, text="Would you like to create an account or login?", fg="black", bg="white")
create = tkinter.Button(window, text="Create an account", fg="white", bg="black")
sign_in = tkinter.Button(window, text="Login", fg="white", bg="black", command=Login)
quit = tkinter.Button(window, text="Quit", fg="white", bg="black", command=window.destroy)
window.configure(background="#008bb5")
photo = tkinter.PhotoImage(file="Eating.gif")
w = tkinter.Label(window, image=photo)
w.pack()
question.pack()
create.pack()
sign_in.pack()
quit.pack()
window.mainloop()
Mainscreen()
The overall aim of the program is to make a scoreboard that displays the scores of both teams and individuals. It needs to contain 4 columns and 5 rows for the team aspect and 20 separate spaces for individual inputs.
I want to make a table that uses user input using the previous box that I had created. So far, I have created up to the point where the user needs to input their team name and displays a table with no entries. I need it to display the events that they are doing as well as the score and their team name/name.
I am new to Python and tkinter so anyone who can help me solve this problem would be great!
from tkinter import *
import tkinter as tk
from time import sleep
def team_name():
print('Okay, what is your team name?')
window = Toplevel(main)
thirdLabel = Label(window, text='Please enter your team name below:',
bg='black', fg='yellow')
thirdLabel.pack()
Label2 = Label(window,text = '')
Label2.pack(padx=15,pady= 5)
entry2 = Entry(window,bd =5)
entry2.pack(padx=15, pady=5)
buttonconfirm1 = Button(window, text='Confirm',command=confirm1)
buttonconfirm1.pack()
def confirm1 ():
window = Toplevel(main)
confirmLabel = Label(window, text='Scoreboard', bg='black',
fg='yellow')
confirmLabel.pack(fill=X)
def table ():
window = Toplevel(main)
label3 = Label(window, text='Team name/Individual name', bg='black',
fg='yellow')
label3.pack()
firstBox = Entry(window, bd=1)
firstBox.pack()
secondBox = Entry(window, bd=1)
secondBox.pack()
thirdBox = Entry(window, bd=1)
thirdBox.pack()
def scoreboard():
window = Toplevel(main)
entry_1 = Label(window, text = '')
entry_2 = Label(window, text = '')
def individual_name():
print('Okay, what is you name?')
window = Toplevel(main)
secondLabel = Label(window, text='Please enter your name below',
bg='black', fg='yellow')
secondLabel.pack()
Label1 = Label(window,text = '')
Label1.pack(padx=15,pady= 5)
entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)
buttonConfirm = Button(window, text='Confirm', command=table)
buttonConfirm.pack()
def confirm ():
window = Toplevel(main)
confirmLabel = Label(window, text='Scoreboard', bg='black', fg='yellow')
confirmLabel.pack(fill=X)
def close():
main.destroy()
main = Tk()
main.maxsize(800,600)
# Labels section
main_menu_label = Label(main, text='Welcome to the scoreboard! Please
select from the options below:', bg='Black',
fg='white')
# Using a photo for top frame on menu and making a label to place it
photo = PhotoImage(file='Scoreboard.gif')
photo_label = Label(main, image=photo)
photo_label.pack(side=TOP)
# Invisible frames for widget placement
top_frame = Frame(main)
top_frame.pack()
bottom_frame = Frame(main)
bottom_frame.pack(side=BOTTOM)
print('Okay! What is your team name?')
# Buttons for main menu and display on the screen
first_button = Button(top_frame, text='Are you entering as an
individual?', fg='yellow', bg='black',
command=individual_name)
second_button = Button(bottom_frame, text='Are you entering as a team?',
fg='yellow', bg='black', command=team_name)
quit_button = Button(bottom_frame, text='Quit?', fg='yellow', bg='black',
command=close)
first_button.pack()
second_button.pack()
quit_button.pack()
main.mainloop()
Sorry if this is a bit confusing, I can't really explain much more than I already have!