I am new to tkinter, I am trying to make a button that flashes green and silver until it is pressed, at which point it reverts to silver. I followed the code from this website, flash button example, it seemed to be closest to what I was trying to do.
%reset -f
import tkinter as tk
root = tk.Tk()
def stop_flash():
print('stop_flash')
root.after_cancel(flasher2)
root.after_cancel(flasher1)
button = tk.Button(root, text="Hello", command=stop_flash, background='silver', activebackground='red')
button.pack()
def flash():
button.configure(background = 'green')
flasher1 = root.after(500, lambda: button.configure(background = 'silver'))
flasher2 = root.after(1000, flash)
flasher1 = root.after(500, lambda: button.configure(background = 'silver'))
flasher2 = root.after(1000, flash)
root.mainloop()
I got the button to flash but I don't understand why it won't stop. I have tried making a separate switch button so I would only need to use 1 after() function but it gets even messier. Any help here would be greatly appreciated!!!!!
Related
So I am working on making an application inside of Tkinter and I am not that experienced in it, but I noticed that when you press a button it moves the text inside down and to the right just a couple pixels and I can't figure out how to make it not do that. Any help finding what parameter I need to change would be greatly appreciated.
I tried finding any docs on the subject but whenever I looked it up I would only get things that pertained to how to place a button on the window or in a frame/canvas with place, grid, pack.
will you can change the relief value to be SUNKEN and adjust the border values like this
Button(root, text = 'Click me !', borderwidth=5, relief=SUNKEN)
the button will look a little weird
or you can use widget other than a button just like this answer (https://stackoverflow.com/a/10674950/20411925)
import tkinter
class main:
def __init__(self,root):
# make a label with some space around the text
self.lbl1 = tkinter.Label(root,
width = 16, height = 4,
text = "Foobar")
self.lbl1.pack()
# Call a function when lbl1 is clicked
# <Button-1> means a left mouse button click
self.lbl1.bind("<Button-1>", self.yadda)
self.lbl1.bind("<Enter>", self.green)
self.lbl1.bind("<Leave>", self.red)
def yadda(self, event):
self.lbl1.config(text="Clicked!")
def green(self, event):
self.lbl1.config(bg="green")
def red(self,event):
self.lbl1.config(bg="red")
if __name__ == "__main__":
root = tkinter.Tk()
main(root)
root.mainloop()
I have a simple program with start and exit buttons. The start button makes a notification using win10toast, but the button remains visibly pressed down and the window becomes unresponsive. The exit button works fine before the start button is pressed. Here's my code:
from tkinter import *
from win10toast import ToastNotifier
root = Tk()
def exit_p():
exit()
def new():
hr.show_toast("New", "Alert")
return
#creates a label widget
myLabel1 = Label(root, text="Full Moon Notification!")
myLabel2 = Label(root, text="Here you can start and exit the program")
button1 = Button(root, text="Start",padx=50,command=new).grid(row=3,column=0)
button2 = Button(root, text="Exit", padx=50,command=exit_p).grid(row=4,column=0)
#puts the widget on the screen
myLabel1.grid(row=0,column=0)
myLabel2.grid(row=1,column=0)
#loop to keep program running
root.mainloop()
The issue is likely because hr.show_toast("New", "Alert") blocks.
The win10toast library conveniently provides an option threaded=True, so just change that code to
hr.show_toast("New", "Alert", threaded=True)
should make it work.
There have already been several topics on Python/Tkinter, but I did not find an answer in them for the issue described below.
The two Python scripts below are reduced to the bare essentials to keep it simple. The first one is a simple Tkinter window with a button, and the script needs to wait till the button is clicked:
from tkinter import *
windowItem1 = Tk()
windowItem1.title("Item1")
WaitState = IntVar()
def submit():
WaitState.set(1)
print("submitted")
button = Button(windowItem1, text="Submit", command=submit)
button.grid(column=0, row=1)
print("waiting...")
button.wait_variable(WaitState)
print("done waiting.")
windowItem1.mainloop()
This works fine, and we see the printout “done waiting” when the button is clicked.
The second script adds one level: we first have a menu window, and when clicking the select button of the first presented item, we have a new window opening with the same as above. However, when clicking the submit button, I don’t get the “Done waiting”. I’m stuck on the wait_variable.
from tkinter import *
windowMenu = Tk()
windowMenu.title("Menu")
def SelectItem1():
windowItem1 = Tk()
windowItem1.title("Item1")
WaitState = IntVar()
def submit():
WaitState.set(1)
print("submitted")
button = Button(windowItem1, text="Submit", command=submit)
button.grid(column=0, row=1)
print("waiting...")
button.wait_variable(WaitState)
print("done waiting")
lblItem1 = Label(windowMenu, text="Item 1 : ")
lblItem1.grid(column=0, row=0)
btnItem1 = Button(windowMenu, text="Select", command=SelectItem1)
btnItem1.grid(column=1, row=0)
windowMenu.mainloop()
Can you explain it?
Inside your SelectItem1 function, you do windowItem1 = Tk(). You shouldn't use Tk() to initialize multiple windows in your application, the way to think about Tk() is that it creates a specialized tkinter.Toplevel window that is considered to be the main window of your entire application. Creating multiple windows using Tk() means multiple main windows, and each one would need its own mainloop() invokation, which is... yikes.
Try this instead:
windowItem1 = Toplevel()
I'm doing a school project. I designed a welcome page for it on tkinter and put a button 'ok' which when pressed moves the code forward but the welcome page doesnt close itself once pressed.
i have tried defining another function to close it but that does not work.
welcome = Tk()
okbutton = Button(welcome, text='ok', command=R)
okbutton.pack()
welcome.mainloop()
and the code moves forward but welcome page remains open...Is there a method to resolve this?
Window never closes automatically when you create new window. You have to use welcome.destroy() for this. You can run it in function which creates new window.
import tkinter as tk
def welcome_page():
global welcome
welcome = tk.Tk()
tk.Label(welcome, text='Welcome').pack()
button = tk.Button(welcome, text='OK', command=other_page)
button.pack()
welcome.mainloop()
def other_page():
global welcome
global other
welcome.destroy() # close previous window
other = tk.Tk()
tk.Label(other, text='Other').pack()
button = tk.Button(other, text='OK', command=end)
button.pack()
welcome.mainloop()
def end():
global other
other.destroy() # close previous window
welcome_page()
To perform the two commands call one command inside the other (sounds like it should be towards the end) and assign the first command to the button.
A button can only call a single function, but that single function can do anything you want.
def do_ok():
print("hello!")
welcome.destroy()
welcome = Tk()
okbutton = Button(welcome, text='ok', command=do_ok)
okbutton.pack()
welcome.mainloop()
TKINTER GRAPHICS
from tkinter import *
tk = Tk()
btn = Button(tk, text="Click Me") #Makes a useless button
btn.pack() #Shows the button
import turtle #Makes the graphics work
t = turtle.Pen()
def hello(): #Makes button not
print('hello here')
btn = Button(tk, text="Click Me", command=hello)
The program SHOULD say hello there when I click the button, but I can't click the button because it won't respond.
As noted in the comments, you appear to create the same button twice, once where it's not connected to a function but packed, and once where it's connected to a function but not packed. If you combine the two, you'll get a working button.
But let's jump in and fix another problem before it begins -- you invoked:
t = turtle.Pen()
No, not when you're working within tkinter. If you're working with turtle standalone, it's Turtle/Pen and Screen, but if you're working within tkinter, it's RawTurtle/RawPen and TurtleScreen. Otherwise you end up with extra windows and potential root conflicts.
Combining all the above together, adding a scrolled canvas for the turtle to play on, and changing the print() to the console to be a turtle.write() to the scrolled canvas, we get:
import tkinter as tk
import turtle # Makes the graphics work
def hello(): # Makes button not
turtle.write('hello there', align='center', font=('Arial', 18, 'normal'))
root = tk.Tk()
button = tk.Button(root, text="Click Me", command=hello) # Makes a button
button.pack() # Shows the button
canvas = turtle.ScrolledCanvas(root)
canvas.pack(side=tk.LEFT)
screen = turtle.TurtleScreen(canvas)
turtle = turtle.RawPen(screen, visible=False)
screen.mainloop()