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.
Related
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!!!!!
I have been recently trying to make a program that it is saved even if you quit the program
The code
import tkinter as tk
root = tk.Tk()
def disabled():
button["state"] = "disabled"
button = tk.Button(root, text="Click me", command=disabled) # when you click the command will disable it
button["state"] = "normal" # first
button.pack()
root.mainloop()
I want from the button to be disabled even if I have escaped
import tkinter as tk
from tkinter import filedialog, Text
from subprocess import call
import os
root = tk.Tk()
def buttonClick():
print('Button is clicked')
def openAgenda():
call("cd '/media/emilia/Linux/Programming/PycharmProjects/SmartschoolSelenium' && python3 SeleniumMain.py",
shell=True)
return
canvas = tk.Canvas(root, height=700, width=700, bg='#263D42')
canvas.pack()
frame = tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text='Open file', padx=10,
pady=5, fg="white", bg='#263D42', command=openAgenda)
openFile.pack()
root.mainloop()
the script it calls opens a new browser window, after finishing entering text in that window, it opens a new browser windows and loops.
meanwhile the tkinter button stays clicked, visually.
the reason your Tk GUI freezes is because you have everything running on 1 thread. The mainloop is haulted by the submit function call which must be taking a "long time", so you probably see "Not Responding" appear in your Tk window when you click the button. To fix this, you need spawn a separate thread for submit to run in, so that the mainloop can keep doing it's thing and keep your Tk window from freezing.
this is done using threading. Instead of your button directly calling submit, have the button call a function that starts a new thread which then starts submit. Then create another functions which checks on the status of the submit thread. You can add a status bar too
import tkinter as tk
from tkinter import filedialog, Text
from subprocess import call
import os
import threading
root = tk.Tk()
def buttonClick():
print('Button is clicked')
def openAgenda():
call("cd ' /media/emilia/Linux/Programming/PycharmProjects/SmartschoolSelenium' && python3 SeleniumMain.py",
shell=True)
canvas.update()
return
def start_Agenda_thread(event):
global Agenda_thread
Agenda_thread = threading.Thread(target=openAgenda)
Agenda_thread.daemon = True
Agenda_thread.start()
canvas = tk.Canvas(root, height=700, width=700, bg='#263D42')
canvas.pack()
frame = tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text='Open file', padx=10,
pady=5, fg="white", bg='#263D42', command=lambda:start_Agenda_thread(None))
openFile.pack()
root.mainloop()
Tkinter is single-threaded: it can only do one thing at a time. While the script is running, the GUI will be frozen. You'll need to do threading, multiprocessing, or find some other way to incorporate that other script in your GUI.
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()
I'm trying to create a pop up message which appears when a button is clicked. Is there any way to do using tkinter in Python 3.4.1?
Try this
import tkinter.messagebox
def onClick():
tkinter.messagebox.showinfo("Title goes here","Message goes here")
root = tkinter.Tk()
button = tkinter.Button(root,text = "Click Me", command = onClick)
button.pack()
root.mainloop()