Tkinter messagebox doesnt disappear after press 'ok/confirmation - python

I wrote a little script to track the battery capacity of my device. Mostly for learning Python.
I decided to use a loop to recognize low capacity. So far it is okay, but when I use a tkinter messagebox, the window does not disappear after I confirm the warning.
import os
from time import sleep
import tkinter as tk
from tkinter import *
from tkinter import messagebox
stat = "/sys_vars/status"
plugged = "PLUGGED"
unplugged = "UNPLUGGED"
batfile = "/sys_vars/capa"
root = tk.Tk()
root.withdraw()
def read(sysfile):
with open(sysfile, "r") as f:
return f.read()
def loopcheck():
plug = str(read(stat).strip())
bat = int(read(batfile))
if plug == plugged:
sleep(2)
if plug == unplugged and 6 <= bat <= 15:
messagebox.showwarning("Alert!","Battery Low!\nCharging Required!")
sleep(2)
if plug == unplugged and bat <= 5:
messagebox.showwarning("ALERT!","Battery ULTRA Low!\nCharging Required!")
sleep(2)
if __name__ == "__main__":
messagebox.showinfo("Running!","Battry Status Tracker is now running on machine")
while True:
loopcheck()
I expect that I confirm the warning and then the message show after the amount of seconds.
The variables and the short sleep-timer in the example are there for tests.

Instead of while True and sleep(2) you should use root.after(2000, loopcheck) to run function in delay. It will not block root.mainloop() which has to run all time to get key/mouse events from system, send events to widgets and redraw windows/widgets.
I don't have these /sys_vars to test it but it could be something like this:
import tkinter as tk
from tkinter import messagebox
stat = "/sys_vars/status"
plugged = "PLUGGED"
unplugged = "UNPLUGGED"
batfile = "/sys_vars/capa"
def read(sysfile):
with open(sysfile, "r") as f:
return f.read()
def loopcheck():
plug = str(read(stat).strip())
bat = int(read(batfile))
if plug == unplugged:
if 6 <= bat <= 15:
messagebox.showwarning("Alert!","Battery Low!\nCharging Required!")
elif bat <= 5:
messagebox.showwarning("ALERT!","Battery ULTRA Low!\nCharging Required!")
root.after(2000, loopcheck)
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
messagebox.showinfo("Running!","Battry Status Tracker is now running on machine")
root.after(2000, loopcheck)
root.mainloop()

Related

pyautogui does not click based on slider/scale values in Tkinter

How I can make pyautogui click based on scale value in Tkinter ?
For example if the scale value is 10 the click delay (with pyautogui.PAUSE) should be of 0.1 it represent 10 clicks per second and if the scale value is 15 the delay should be of 0.15 => 15 clicks per second.
The problem clicks are not activated depending on the scale value
I also can't put root.mainloop() anywhere else than in the While True loop although I would have liked to put it at the very end of the program but in this case I would have to replace while True by "while "normal" == root.state()" (to stop the loop as soon as the Tkinter window closes) which doesn't work either
The code :
import mouse
import threading
import pyautogui
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry('500x400')
bovar = IntVar()
def func(*args):
if bovar.get() == 10:
pyautogui.PAUSE = 0.1
elif bovar.get() == 11:
pyautogui.PAUSE = 0.11
elif bovar.get() == 12:
pyautogui.PAUSE = 0.12
elif bovar.get() == 13:
pyautogui.PAUSE = 0.13
elif bovar.get() == 14:
pyautogui.PAUSE = 0.14
else:
pyautogui.PAUSE = 0.15
scale = Scale(root,orient=HORIZONTAL,from_=10,to=15,variable=bovar,command=func)
label = Label(root,text='CPS:')
label.pack(side=TOP)
scale.pack()
func()
root.mainloop()
def repeat_function(kill_event):
while not kill_event.is_set():
pyautogui.click()
while True:
kill_event = threading.Event()
new_thread = threading.Thread(target=lambda: repeat_function(kill_event))
mouse.on_button(new_thread.start, (), mouse.RIGHT, mouse.DOWN)
mouse.on_button(kill_event.set, (), mouse.RIGHT, mouse.UP)
mouse.wait(mouse.RIGHT, mouse.UP)
mouse.unhook_all()
root.mainloop() starts an infinite loop that waits for actions on the tkinter window and responds to them. Thus, any code you write under this call isn't going to execute until you close tkinter. If you want something to run simultaneously with this tkinter loop, it will have to run in another thread. Your logic of determining PAUSE is also a little flawed. A pause of 0.15 seconds doesn't mean it will click 15 times per second, but 1/0.15 times.
import mouse
import threading
import pyautogui
from tkinter import *
from tkinter import ttk
def convert_to_pause(clicks_per_second):
clicks_per_second = min(clicks_per_second, 15)
return 1 / clicks_per_second
def repeat_function(kill_event, int_var):
while not kill_event.is_set():
pyautogui.PAUSE = convert_to_pause(int_var.get())
pyautogui.click()
def hotkey_thread_loop(int_var):
# The loop you already have, we just need to run it in a separate thread
# We pass it the IntVar from the Scale so that it can dynamically read its value
while True:
kill_event = threading.Event()
# Here we also pass the IntVar to access its value in the new thread
new_thread = threading.Thread(target=lambda: repeat_function(kill_event, int_var))
mouse.on_button(new_thread.start, (), mouse.RIGHT, mouse.DOWN)
mouse.on_button(kill_event.set, (), mouse.RIGHT, mouse.UP)
mouse.wait(mouse.RIGHT, mouse.UP)
mouse.unhook_all()
# Start the main program
if __name__ == "__main__":
# Set up all tkinter stuff
root = Tk()
root.geometry('500x400')
bovar = IntVar()
scale = Scale(root,orient=HORIZONTAL,from_=10,to=15,variable=bovar,command=func)
label = Label(root,text='CPS:')
label.pack(side=TOP)
scale.pack()
# Create the thread to manage hotkey pressing and start it
# Set daemon=True to have it terminate when main program ends
hotkey_thread = threading.Thread(target=lambda: hotkey_thread_loop(bovar),
daemon=True)
hotkey_thread.start()
# Start the tkinter application, the ininite loop
root.mainloop()
# Any code put here (below mainloop()) will not execute while tkinter runs,
# don't try to implement more logic here

python thread data manipulation

I am trying to manipulate some data in a thread from the main function. The issue I am facing about modifying some of the variables which are part of the function which is running on a thread.
So I am trying to run a Tkinter based GUI loop in a thread to ensure it is always running. And want to modify some of the label corresponding to the status in the main function execution. I am facing an issue where it is unable to locate the label variables in the main loop as it is part of the function running on the thread.
Below is a simplified psuedo code for that approach. Please suggest if this a correct way to the above task or is there is any better and efficient way.
import threading
def thread_func():
i = 0
while True:
print('i from thread: ', i)
if __name__ == '__main__':
t = threading.Thread(target=thread_func)
t.start()
while True:
i += 1
Actual scaled down simplified code
import threading
import tkinter as tk
def gui():
window = tk.Tk()
label = tk.Label(text='On')
label.pack()
window.mainloop()
if __name__ == '__main__':
t = threading.Thread(target=gui)
t.start()
while True:
label['text'] = 'Active'
Error:
Traceback (most recent call last):
File "test.py", line 17, in <module>
label['text'] = 'Active'
NameError: name 'label' is not defined
Is there a better way to keep the tkinter gui always on and perform some task in the loop?
Using class and threading:
import tkinter
class Test(tkinter.Label):
x = False
def __init__(self):
super().__init__()
self.pack()
self['text'] = 'On'
def gui(self):
if not self.x:
self['text'] = 'Active'
self.mainloop()
else:
self.mainloop()
if __name__ == '__main__':
label = Test()
while isinstance(label, Test):
t = threading.Thread(target=label.gui())
t.start()
When you write code of label there then you will get error because when program start it starts from creating thread, that thread will only end when tkinter window is close and same for previous thread_fuc code. And you wrote the label code after tkinter window is closed.
The above issue will be solved by doing this :
import threading
def thread_func():
while True:
print('i from thread: ', i)
def tt():
global i
while True:
i += 1
if __name__ == '__main__':
i=0
t = threading.Thread(target=thread_func)
t.start()
yt = threading.Thread(target=tt)
yt.start()
Making i global and running 2 function parallely. We have to global because we can't use the variable of one function to another. And additionally we are running 2 function in 2 thread.
And for your tkinter file as #TheLizzard suggest, you can use .after insted of using thread in tkinter if you want to change content constantly/want to use loop.
Here's the basic example how you can implement it:
import random
import tkinter as tk
app = tk.Tk()
app.geometry("200x220")
label = tk.Label(app, text="0")
label.pack()
def change(b=0):
if b < 30:
a = random.randrange(1, 7, 1)
label.config(text=a)
app.after(100, change, b+1)
b1 = tk.Button(app, text="Get New Number", command=change)
b1.pack()
app.mainloop()
For more explanation about it you may visit here.

TkInter Frame doesn't load if another function is called

I'm writing a Python programme which listens for RFID input and only runs if a valid token is presented. The programme also has a GUI which I'm wanting to build using TkInter.
Both parts of the puzzle work fine in isolation, however as it stands I seem to be able to choose one or the other - but not both! I can draw my TkInter window fine, however if I call the function to start listening for the RFID input then whilst that bit runs OK and works... there's no GUI.
Code is below. You can see my debugging efforts so far with my printouts to the terminal...
#!/usr/bin/env python3
import sys
import MySQLdb
if sys.version_info[0] == 2:
from Tkinter import *
import Tkinter as ttk
else:
from tkinter import *
import tkinter as ttk
class Fullscreen_Window:
def __init__(self):
self.tk = Tk()
self.frame = Frame(self.tk)
self.frame.pack()
ttk.Button(self.tk, text="hello world").pack()
self.tk.attributes('-zoomed', True)
self.state = False
self.tk.bind("<F11>", self.toggle_fullscreen)
self.tk.bind("<Escape>", self.end_fullscreen)
print("init running")
self.listen_rfid() # Commenting this out makes the GUI appear, uncommenting means no GUI :(
def toggle_fullscreen(self, event=None):
self.state = not self.state # Just toggling the boolean
self.tk.attributes("-fullscreen", self.state)
print("Toggling")
return "break"
def end_fullscreen(self, event=None):
self.state = False
self.tk.attributes("-fullscreen", False)
return "break"
def listen_rfid(self):
print("Main loop running")
dbHost = 'localhost'
dbName = 'python'
dbUser = 'python'
dbPass = 'PASSWORD'
dbConnection = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPass, db=dbName)
cur = dbConnection.cursor(MySQLdb.cursors.DictCursor)
with open('/dev/stdin', 'r') as tty:
while True:
RFID_input = tty.readline().rstrip()
cur.execute("SELECT * FROM access_list WHERE rfid_code = '%s'" % (RFID_input))
if cur.rowcount != 1:
print("ACCESS DENIED")
else:
user_info = cur.fetchone()
print("Welcome %s!!" % (user_info['name']))
tty.close()
listen_rfid()
if __name__ == '__main__':
w = Fullscreen_Window()
w.tk.mainloop()
I'm sure it's something really simple but as I'm a Python/TkInter n00b it's beaten me and I'm all done Googling. Any help gratefully received :)
Tkinter (and all GUIs) has an infinite loop called the mainloop that keeps the GUI active and responsive. When you make another infinite loop (while True) you block Tkinter's mainloop; and the GUI fails. You need to either put your loop in a separate thread or use Tkinter's mainloop to do your work. Since you are using a blocking readline, the thread is the best way to go. As a guess, replace your call with this:
from threading import Thread
t = Thread(target=self.listen_rfid)
t.daemon = True # this line tells the thread to quit if the GUI (master thread) quits.
t.start()
Edit: BTW, your imports are very bad. "ttk" is a subset of tkinter, not an alias, the alias "tk" is usually used for tkinter, and wildcard imports are bad and should be avoided. This is how your tkinter imports should look:
try:
# python 2
import Tkinter as tk
import ttk
except ImportError:
# python 3
import tkinter as tk
from tkinter import ttk
And then you use the appropriate prefix:
self.tk = tk.Tk()
self.frame = tk.Frame(self.tk)
You should run listen_rfid using after. The problem is that listen_rfid as you have written it will run forever meaning that mainloop never starts. If you do this:
#!/usr/bin/env python3
import sys
import select
import MySQLdb
if sys.version_info[0] == 2:
from Tkinter import *
import Tkinter as ttk
else:
from tkinter import *
import tkinter as ttk
class Fullscreen_Window:
def __init__(self):
self.tk = Tk()
self.frame = Frame(self.tk)
self.frame.pack()
ttk.Button(self.tk, text="hello world").pack()
self.tk.attributes('-zoomed', True)
self.state = False
self.tk.bind("<F11>", self.toggle_fullscreen)
self.tk.bind("<Escape>", self.end_fullscreen)
print("init running")
# Schedule self.listen_rfid to run after the mainloop starts
self.tk.after(0, self.listen_rfid)
def toggle_fullscreen(self, event=None):
self.state = not self.state # Just toggling the boolean
self.tk.attributes("-fullscreen", self.state)
print("Toggling")
return "break"
def end_fullscreen(self, event=None):
self.state = False
self.tk.attributes("-fullscreen", False)
return "break"
def listen_rfid(self):
print("Main loop running")
dbHost = 'localhost'
dbName = 'python'
dbUser = 'python'
dbPass = 'PASSWORD'
dbConnection = MySQLdb.connect(host=dbHost, user=dbUser, passwd=dbPass, db=dbName)
cur = dbConnection.cursor(MySQLdb.cursors.DictCursor)
# readline is blocking so check that there is input
# before attempting to read it.
r, w, x = select.select([sys.stdin], [], [], 0)
if r:
# There is available input, so read a line.
RFID_input = sys.stdin.readline().rstrip()
cur.execute("SELECT * FROM access_list WHERE rfid_code = '%s'" % (RFID_input))
if cur.rowcount != 1:
print("ACCESS DENIED")
else:
user_info = cur.fetchone()
print("Welcome %s!!" % (user_info['name']))
# keep running every 500 milliseconds for as long as
# the mainloop is active.
self.tk.after(500, self.listen_rfid)
if __name__ == '__main__':
w = Fullscreen_Window()
w.tk.mainloop()
it will check every half second whether there is some input on the command line and process it.

I want to toggle a real pushbutton and display it on tkinter GUI

I want to toggle a pushbutton and show its changes on a label using tkinter.
If I press the button it shows "on" on the label and when I press again it shows "off" on the label
So I try these codes and If I'm trying the wrong code please help me write the correct using tkinter.
I have a problem in combining this code
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22,GPIO.IN,up_down=GPIO.PUD_UP)
while(1):
if GPIO.input(22)==1:
if bs == False :
x.set("on")
bs=True
sleep(0.5)
else:
x.set("off")
bs=False
sleep(0.5)
This works okay but I want to connect it to a GUI label to print on it on or off.
Here is the tkinter code
import tkinter.*
root = tk()
x = StringVar()
s=Label(root,textvariable=x)
s.grid(column=0,row=0)
root.mainloop()
When I try to combine it I make it like this
from Tkinter import *
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)
b=False
def check_button1():
if GPIO.input(7)== 1:
if b == False :
labelText1.set("on")
print"on"
b=True
time.sleep(0.5)
else:
labelText1.set("off")
print"off"
b=False
time.sleep(0.5)
mamdouh.after(10,check_button1)
mamdouh = Tk()
labelText1 = StringVar()
x1 = Label(mamdouh,textvariable=labelText1)
x1.config(font=('Helvetica',25,'bold'))
x1.grid(row=0,column=0)
mamdouh.title("mamdouh")
mamdouh.geometry('1200x700')
mamdouh.after(10,check_button1)
mamdouh.mainloop()
but it didn't works it keeps blank every time I press the push button actually If it works well I will put 17 push button
I think that the problem is in placing this if statment on the right place and placing the b variable in it's right place and I think also there is a problem between this if statment and tkinter because I tried this code wich works perfect but it is not toggling the push button so I want to change this lets add this code here also :
from Tkinter import *
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)
def check_button1():
if(GPIO.input(7) == GPIO.LOW):
labelText1.set("on")
else:
labelText1.set("off")
mamdouh.after(10,check_button1)
mamdouh = Tk()
labelText1 = StringVar()
x1 = Label(mamdouh,textvariable=labelText1)
x1.config(font=('Helvetica',25,'bold'))
x1.grid(row=0,column=0)
mamdouh.title("mamdouh")
mamdouh.geometry('1200x700')
mamdouh.after(10,check_button1)
mamdouh.mainloop()
So how I can make this toggle push button on an Label?
Your problem is recognizing button down and button up events. Your OS mouse driver does this for your mouse buttons. If your GPIO module does not do this for you, you will have to detect these events by comparing the current state to the previous state. (I am ignoring here any possible need to 'de-bounce' the button.) You are sort of trying to do this with the time.sleep(.5) calls, but do not use time.sleep in gui code.
Your driver should be self-contained and independent of any tk widgets other than the root needed for .after. For multiple buttons, you will need your own GPIOButton class. Your code that works is a starting point. Tkinter allows you to tie a command to button-up events. Your class init should similarly take up and or down event commands (callbacks).
Here is something untested that might get you started.
class GPIOButton:
def __init__(self, master, buttons, command_down=None, command_up=None):
self.master = master
self.buttons = buttons
self.command_down = command_down
self.command_up = command_up
GPIO.setmode(GPIO.BOARD)
for button in buttons:
GPIO.setup(button, GPIO.IN)
self.state = [GPIO.HIGH] * len(buttons) # best initial value?
self.check_buttons() # or call this elsewhere
def check_buttons(self):
for i, button in enumerate(self.buttons):
oldstate = self.state[i]
newstate = GPIO.input(button)
if oldstate != newstate:
self.state[i] = newstate
command = (self.command_down if newstate==GPIO.LOW
else self.command_up)
command(button)
self.master.after(10, self.check_button)
Let me preface my answer with a disclaimer—I don't have a Raspberry Pi, so couldn't verify this works with the real thing. For testing I used a proxy class that simulates random button pressing. You may have to adjust the DELAY value depending on how fast the GPIO interface works.
However, I have put commented-out code in near the top showing what I think you would need to use based on of doing so in your code.
try:
import Tkinter as tk
import tkFont
except ImportError: # Python 3
import tkinter as tk
import tkinter.font as tkFont
#import RPi.GPIO as GPIO
#
#GPIO.setmode(GPIO.BOARD)
#
#class GPIOButton(object):
# """ Encapsulates GPIO button interface. """
# def __init__(self, pin):
# self.pin = pin
# self.status = 0
# GPIO.setup(pin, GPIO.IN)
#
# def update_status(self):
# self.status = GPIO.input(pin) == GPIO.LOW
### Proxy class since I don't have a Rasperry Pi. ###
import random
class GPIOButton(object):
def __init__(self, pin):
self.pin = pin
self.status = 0
def update_status(self):
if not random.randint(0, 99) % 20: # occassionally toggle status
self.status = not self.status
class App(tk.Frame):
STRIDE = 8
DELAY = 100 # delay in millsecs between button status updates
def __init__(self, gpio_buttons, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.gpio_buttons = gpio_buttons
self.create_widgets()
self.after(self.DELAY, self.update_buttons, self.DELAY) # start updates
def create_widgets(self):
self.btn_font = tkFont.Font(family="Helvetica", size=12, weight='bold')
self.gui_buttons = []
for i, button in enumerate(self.gpio_buttons):
is_pressed = tk.BooleanVar()
is_pressed.set(False)
radiobutton = tk.Radiobutton(self,
text=format(i+1, '02d'),
font=self.btn_font,
value=True,
variable=is_pressed,
relief=tk.RIDGE)
row, col = divmod(i, self.STRIDE)
radiobutton.grid(column=col, row=row)
self.gui_buttons.append(is_pressed)
def update_buttons(self, delay):
for i, gpio_button in enumerate(self.gpio_buttons):
previous_status = gpio_button.status
gpio_button.update_status()
if gpio_button.status != previous_status:
self.gui_buttons[i].set(gpio_button.status)
self.after(delay, self.update_buttons, delay) # rinse and repeat
gpio_buttons = [GPIOButton(pin) for pin in range(16)]
app = App(gpio_buttons)
app.master.title('Rasberry Pi Buttons')
app.mainloop()
Here's what the simulation looks like running on my Windows computer:

Opening toplevel windows from inside a loop

I have the following code:
import time, os
from tkinter import *
class Chat():
def sPrint():
s=0
while s < 11:
s+=1
print (s, 'Sec')
time.sleep(1)
if s ==5:
print ("5..")
myapp.OpenWindow1()
if s ==10:
print ("10..")
myapp.OpenWindow2()
class App(Frame):
def ConnectButton1(self):
self.con1 = Button(self)
self.con1["text"] = "Connect1",
self.con1["command"] = lambda: Chat.sPrint()
self.con1.grid(row=0,column=2,padx=5, pady=3,sticky=W)
def OpenWindow1(self):
win1 = Toplevel()
def OpenWindow2(self):
win2 = Toplevel()
myapp = App()
if __name__ == "__main__":
myapp.ConnectButton1()
myapp.pack()
myapp.mainloop()
Problem is - the "print" works perfect every 5 seconds from inside the loop,
but the Toplevel functions runs only when the "while" end (and then shows two
toplevel windows at the same time)
Same happen when i use the
threading.Timer(1, sPrint1).start()
for running a function in a loop.. i can't load a new tkinter buttons/labels
while the function is in a loop.
Any suggestions?

Categories