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'))
Related
import tkinter
app = tkinter.Tk()
app.geometry("1080x2400")
title = tkinter.Label(
app, text="contador de sets y descansos", font=("Arial", 30))
title.pack(side=tkinter.TOP, fill=tkinter.BOTH)
design = tkinter.Label(app, bg="white")
design.pack(fill=tkinter.BOTH, expand=True)
def series():
label = tkinter.Label(sets, text="completed a set")
label.pack()
sets = tkinter.Button(design, text="sets", padx=40,
pady=20, font=("Arial", 20), command=series)
sets.pack(side=tkinter.LEFT)
def descanso():
label = tkinter.Label(rest, text="completed rest")
label.pack()
rest = tkinter.Button(design, text="rest", padx=40,
pady=20, font=("Arial", 20), command=descanso)
rest.pack(side=tkinter.RIGHT,)
app.mainloop()
I tried to make a button that displays a message on the GUI, but when I press it, the button disappears and what I want is the button to display the message and stay there
Use app for all in line 17, 21, 27 and 31. You had this Button(design,. Should be Button(app,
Here is code:
import tkinter
app = tkinter.Tk()
app.geometry("1080x2400")
title = tkinter.Label(
app, text="contador de sets y descansos", font=("Arial", 30))
title.pack(side=tkinter.TOP, fill=tkinter.BOTH)
design = tkinter.Label(app, bg="white")
design.pack(fill=tkinter.BOTH, expand=True)
def series():
label = tkinter.Label(app, text="completed a set")
label.pack()
sets = tkinter.Button(app, text="sets", padx=40,
pady=20, font=("Arial", 20), command=series)
sets.pack(side=tkinter.LEFT)
def descanso():
label = tkinter.Label(app, text="completed rest")
label.pack()
rest = tkinter.Button(app, text="rest", padx=40,
pady=20, font=("Arial", 20), command=descanso)
rest.pack(side=tkinter.RIGHT,)
app.mainloop()
Output:
I am working on a simple counter app in tkinter. I rigged up some code looking at few tutorial on web. All the functions of a counter are set up. But when it comes to the designing of the app, I want the Count, the Count button, and the reset button to be aligned at the center.
The code is as below
from tkinter import Label, Button, Tk
from tkinter import font
window = Tk()
window.geometry('500x500')
window.title("Counter")
window.count = 0
def increment():
window.count += 1
lbl.configure(text=window.count)
def reset():
window.count = 0
lbl.configure(text=window.count)
lbl = Label(window, text="0", font=("Apple Braille", 60))
lbl.grid(column=0, row=0)
btn1 = Button(window, text="Count", command=increment)
btn1.grid(column=0, row=1)
btn2 = Button(window, text="Reset", command=reset)
btn2.grid(column=1, row=1)
btn1['font'] = btn2['font'] = font.Font(size=30)
window.mainloop()
A Screenshot of my counter app is here
Any help in this aspect will be appreciated.
Thanks,
It is easier to use pack() instead of grid() for your requirement.
lbl = Label(window, text="0", font=("Apple Braille", 60))
lbl.pack()
# frame for the two buttons
frame = Frame(window)
frame.pack()
btn1 = Button(frame, text="Count", command=increment)
btn1.grid(column=0, row=1)
btn2 = Button(frame, text="Reset", command=reset)
btn2.grid(column=1, row=1)
If you want to put at the center of the window:
# frame for the label and buttons
frame = Frame(window)
frame.place(relx=0.5, rely=0.5, anchor="c") # put at center of window
lbl = Label(frame, text="0", font=("Apple Braille", 60))
lbl.grid(row=0, column=0, columnspan=2)
btn1 = Button(frame, text="Count", command=increment)
btn1.grid(column=0, row=1)
btn2 = Button(frame, text="Reset", command=reset)
btn2.grid(column=1, row=1)
I am using python 3.7 and tkinter for making a GUI which saves all my important passwords which are saved in a file passwords.txt. In this I want to create a button in my main window which pops up another window with an entry box and a button(which will close the window) and till this window is not closed it will not let the user to interact with my old window.
Here's my codes:
from tkinter import *
from tkinter import ttk
root = Tk()
f = open("passwords.txt", "r")
list1 = []
for item in f.readlines():
item = item.replace("\n", "")
list1.append(item)
def secondwindow():
root2 = Tk()
root2.title("Secure Your Password")
root2.configure(bg="black")
root2.geometry('700x600')
frame_color = "#%02x%02x%02x" % (150,150,150)
# Create A Main frame
main_frame = Frame(root2, bg=frame_color)
main_frame.pack(fill=BOTH,expand=1)
# Create Frame for X Scrollbar
sec = Frame(main_frame, bg=frame_color)
sec.pack(fill=X,side=BOTTOM)
# Create A Canvas
my_canvas = Canvas(main_frame, bg="black")
my_canvas.pack(side=LEFT,fill=BOTH,expand=1)
# Add A Scrollbars to Canvas
x_scrollbar = ttk.Scrollbar(sec,orient=HORIZONTAL,command=my_canvas.xview)
x_scrollbar.pack(side=BOTTOM,fill=X)
y_scrollbar = ttk.Scrollbar(main_frame,orient=VERTICAL,command=my_canvas.yview)
y_scrollbar.pack(side=RIGHT,fill=Y)
# Configure the canvas
my_canvas.configure(xscrollcommand=x_scrollbar.set)
my_canvas.configure(yscrollcommand=y_scrollbar.set)
my_canvas.bind("<Configure>",lambda e: my_canvas.config(scrollregion= my_canvas.bbox(ALL)))
# Create Another Frame INSIDE the Canvas
second_frame = Frame(my_canvas, bg=frame_color)
# Add that New Frame a Window In The Canvas
my_canvas.create_window((0,0),window=second_frame, anchor="nw")
f = Frame(second_frame, borderwidth=2, relief=SUNKEN, bg=frame_color)
f.pack(side=TOP, fill=X)
Label(f, text="Secure Your Password", fg="white", bg=frame_color, font="Algerian 35 italic").pack()
f1 = Frame(second_frame, bg="black")
f1.pack(fill=BOTH, side=TOP, expand=1)
Label(f1, text="Application", fg="red", bg="black", font="Calibri 20 bold", pady=10, padx=60).grid(row=1, column=1)
Label(f1, text="Username", fg="red", bg="black", font="Calibri 20 bold", pady=10, padx=210).grid(row=1, column=2)
Label(f1, text="Password", fg="red", bg="black", font="Calibri 20 bold", pady=10, padx=198).grid(row=1, column=3, padx=140)
for i in range(len(list1)):
application = list1[i].split(";;;")[0]
username = list1[i].split(";;;")[1]
password = list1[i].split(";;;")[2]
Label(f1, text=application, fg="white", bg="black", font="Calibri 20 bold", pady=5).grid(row=i+2, column=1)
Label(f1, text=username, fg="white", bg="black", font="Calibri 20 bold", pady=5).grid(row=i+2, column=2)
Label(f1, text=password, fg="white", bg="black", font="Calibri 20 bold", pady=5).grid(row=i+2, column=3)
root2.mainloop()
def checkPassword(password, l):
if password == "a":
root.destroy()
secondwindow()
else:
l.config(text="Wrong Password")
def password_window():
root.geometry('450x270')
root.title("Secure Your Password")
root.minsize(450, 270)
root.maxsize(450, 270)
root.configure(bg="black")
Label(root, text="Secure Your Password", fg="white", bg="black", font="Algerian 24 italic").pack(side=TOP)
Label(root, text="Your Password", fg="white", bg="black", font="Clibri 15").pack(pady=10)
password = StringVar()
Entry(root, textvariable=password, bg="grey", fg="white", font="Calibri 15 bold").pack(pady=10)
Button(root, text="Login", bg="grey", fg="white", activebackground="grey", font="Calibri 10", command=lambda: checkPassword(password.get(), l)).pack(pady=8)
l = Label(root, fg="red", bg="black", font="Clibri 10 bold")
l.pack()
password_window()
root.mainloop()
And my passwords.txt:
StackOverflow;;;PomoGranade;;;PomoGranade_StackOverflow
GitHub;;;Pomogranade;;;PomoGranade_GitHub
I am new to python and tkinter. Thanks for help in advance :)
I do not recommend using * imports, though it may not be exactly wrong in this case.
Use the TopLevel widget instead of initialising another Tk window. See why using another Tk is not good.
Use .grab_set() (Look at #TheLizzard's link in the comment for a better example)
Look at this example -
import tkinter as tk
root = tk.Tk()
def f1():
top1 = tk.Toplevel(root)
b2 = tk.Button(top1,text='Close New Window',command=top1.destroy)
b2.pack()
top1.grab_set()
b1 = tk.Button(root,text='Create Mandatory Window',command=f1)
b1.pack()
root.mainloop()
If you run this code, you will see that the first window does not react to any mouse press etc... and also you cannot close the first window after opening the new window until the it is closed
I'm new to Python GUI and trying to learn using tkinter, I have created a main window on clicking the button "Continue" in the main window, a top level window opens and a number can be entered in the entry box, I'm not able to read the value in main window. Can anybody help me with how to read the value entered in the top level window and enter the same in the main window
from tkinter import *
# global variable
blank_2 = []
blank_1 = []
# Function to create a top level window with entry boxes
def open_new_window():
# Toplevel object which will be treated as a new window
popup = Toplevel(main)
global blank_2
global blank_1
# sets the title of the Toplevel widget
popup.title("Enter the value")
# sets the geometry of toplevel
popup.geometry("480x220")
popup.geometry("+500+250")
popup.configure(background='grey')
PL = Label(popup, text="Enter the 1st value :", fg="white", bg="grey",
font=('century gothic', 12))
PL.place(x=20, y=20)
GW = Label(popup, text="Enter the 2nd value :", fg="white", bg="grey",
font=('century gothic', 12))
GW.place(x=20, y=70)
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_2.place(x=210, y=20)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1.place(x=210, y=70)
exitp = Button(popup, text='Quit', font=8, width=5, height=1, bd=4, command=popup.destroy)
setp = Button(popup, text='Set', font=8, bd=4, command=sum)
exitp.place(x=300, y=120)
setp.place(x=240, y=120)
blank_2.focus()
# Function to insert the value from the top level window to main window
def sum():
global blank_2, blank_1
LS.insert(END, blank_2)
LS.insert(END, blank_1)
main = Tk()
main.title("TEST") # required text on window title
main.configure(background='grey') # Background color of Main window
main.geometry("+600+150") # Position of the window on the screen
main.geometry("400x450") # Size of the window
# text window to display the value in main window
LS = Text(main, bd=3, height=3, width=30, bg="light cyan")
LS.place(x=20, y=190)
# buttons to open a top level window and to quit the main window
C = Button(main, text='Continue', font=8, bd=4, command=open_new_window)
C.place(x=40, y=45)
Q = Button(main, text='Quit', font=8, width=5, height=1, bd=4, command=main.destroy)
Q.place(x=150, y=45)
mainloop()
The output in the main window
You need to call the get method of the Entry´s like
LS.insert(END, blank_2.get())
LS.insert(END, blank_1.get())
since you defined blank_1 and blank_2 as Entry´s:
blank_2 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
blank_1 = Entry(popup, justify=LEFT, font=12, fg="black", width=15)
.!toplevel.!entry
is the reference for tkinter to the wdiget.
how do I position my label which says "Question One" in my def new_window() function. As you run it the label is being positioned at the bottom, And i want it to be applied on the top.
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=150, height=40)
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display.pack()
message.pack()
display2 = Label(root, width=100, height=30, bg='green')
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label1 = Label(text="A Quiz Game", font = ("Arial", "24"), padx=40,
pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
display2.pack()
button1.pack()
message_label1.pack()
message_label2.pack()
root.mainloop() # Runs the main window loop
You are packing in the wrong order. Do not pack display before your message. So just swapping the order will fix the issue.
Here is the code. Replace your def new_window(): with this
def new_window():
newWindow = Toplevel()
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display = Label(newWindow, width=150, height=40)
message.pack()
display.pack()
pack method just blindly packs the widget into the window. And the next pack will be done below it if there is space. So take care of the order while packing widgets :)