is there a way to automatically run a function besides my main.py every hour. Using while loop, it does not work, because my main.py is a tkinter class, which after its initialization must not remain "trapped" in a while loop.
This is a initialization part of my main.py:
class ChatApp:
index = 0
def __init__(self):
self.create_index()
self.window = Tk()
self._setup_main_window()
self.start_model()
...
if __name__ == "__main__":
app = ChatApp()
app.run()
You can run a separate thread which waits an hour between executing.
Although it depends on what you want the task to be.
import threading
import time
def func():
while True:
#do something
time.sleep(3600)
t1 = threading.thread(target=func)
class ChatApp:
index = 0
def __init__(self):
self.create_index()
self.window = Tk()
self._setup_main_window()
self.start_model()
...
if __name__ == "__main__":
t1.start()
app = ChatApp()
app.run()
Related
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.
I'm making a game where I would like a timer to be displayed on the screen after the user clicks on 'NEW GAME', to keep track of how long they've been playing. I have a class that runs the timer fine by itself, but when I incorporate it into the rest of my game and then on top of that, try to display the updated values of the timer, no values in the UI are updated and the printout of the timer doesn't even occur in the terminal. I've tried running the timer in the same thread as the game-setup process and I've also tried creating a new thread to run the timer but neither work. The game loads up and functions fine, with the exception of the timer not counting upwards and not displaying the updated timer values. Where am I going wrong here?
Here is my standalone Timer class, which again, works fine by itself.
from PyQt5 import QtCore
import sys
def startThread(functionName, *args, **kwargs):
print(args)
if len(args) == 0:
t = threading.Thread(target=functionName)
else:
try:
t = threading.Thread(target=functionName, args=args, kwargs=kwargs)
except:
try:
if args is None:
t = threading.Thread(target=functionName, kwargs=kwargs)
except:
t = threading.Thread(target=functionName)
t.daemon = True
t.start()
class Timer(object):
def __init__(self):
super(Timer, self).__init__()
def start_timer(self):
print("Starting timer...")
Timer.timer = QtCore.QTimer()
Timer.time = QtCore.QTime(0, 0, 0)
Timer.timer.timeout.connect(self.tick)
Timer.timer.start(1000)
def tick(self):
Timer.time = Timer.time.addSecs(1)
self.update_UI('%s' % Timer.time.toString("hh:mm:ss"))
def update_UI(self, text_string):
print(text_string)
# This is where the text would be sent to try and update the UI
Timer().start_timer()
This is more or less how my game-setup class is structured - currently I'm showing the version that uses threading:
class BuildUI(PyQt5.QtWidgets.QMainWindow, Ui_game):
def __init__(self):
super(BuildUI, self).__init__()
self.setupUi(self)
self.easy_mode = 38
self.user_available_cells = []
self.start_easy_game.triggered.connect(lambda: self.setup_game(self.easy_mode))
def setup_game(self, hidden_count):
def create_game_board():
startThread(Timer().start_timer)
self.game = BuildGame()
#The BuildGame class is not deliberately not shown
startThread(create_game_board)
class GAME(object):
def __init__(self):
GAME.app = PyQt5.QtWidgets.QApplication(sys.argv)
GAME.UI = BuildUI()
GAME.UI.show()
GAME.app.exec_()
def main():
GAME()
if __name__ == '__main__':
main()
The key to getting this to work is by using signals. Leaving the Timer class exactly as it is, the only modifications to be done are in the initialization of the GAME class, a signal needs to be added at the beginning of the BuildUI class and then using emit() to trigger that signal just before the self.game = BuildGame() call.
class BuildUI(PyQt5.QtWidgets.QMainWindow, sudoku_ui.Ui_sudoku_game):
# This signal just triggers a msgbox to display, telling the user the game is loading
process_start = PyQt5.QtCore.pyqtSignal()
# this is called to automatically close the msgbox window
process_finished = PyQt5.QtCore.pyqtSignal()
# This signal, when called will start the timer
start_game_timer = PyQt5.QtCore.pyqtSignal()
def __init__(self):
super(BuildUI, self).__init__()
self.setupUi(self)
self.easy_mode = 38
self.user_available_cells = []
self.start_easy_game.triggered.connect(lambda: self.setup_game(self.easy_mode))
def setup_game(self, hidden_count):
def create_game_board():
self.game = BuildGame()
# Now that the game is built, the timer can start
# This is the emit which will start the timer
GAME.UI.start_game_timer.emit()
startThread(create_game_board)
class GAME(object):
def __init__(self):
GAME.app = PyQt5.QtWidgets.QApplication(sys.argv)
GAME.UI = BuildUI()
GAME.dialog_box = MsgPrompt()
# This is the key right here - initializing the timer class
GAME.timer = Timer()
# This line below attaches a function to the emit() call - which will kick off the timer
GAME.UI.start_game_timer.connect(GAME.timer.start_timer)
# The below referenced class is deliberately omitted from this post
GAME.UI.process_start.connect(GAME.dialog_box.show_dialog_box)
GAME.UI.process_finished.connect(GAME.dialog_box.hide_dialog_box)
GAME.UI.show()
GAME.app.exec_()
def main():
GAME()
if __name__ == '__main__':
main()
I am trying to load some data which takes 30+ seconds. During this time I wish the user to see a small GUI which says "Loading .", then "Loading ..", then "Loading ...", then "Loading ." etc. I have done some reading and I think I have to put this in a separate thread. I found someone who had a similar problem suggesting the solution was this in the right spot:
t = threading.Thread(target=self.test)
t.daemon = True
t.start()
In a lower part of the file I have the test function
def test(self):
tmp = InfoMessage()
while True:
print(1)
and the InfoMessage function
from PyQt5 import uic, QtCore, QtGui, QtWidgets
import sys
class InfoMessage(QtWidgets.QDialog):
def __init__(self, msg='Loading ', parent=None):
try:
super(InfoMessage, self).__init__(parent)
uic.loadUi('ui files/InfoMessage.ui',self)
self.setWindowTitle(' ')
self.o_msg = msg
self.msg = msg
self.info_label.setText(msg)
self.val = 0
self.timer = QtCore.QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.update_message)
self.timer.start()
self.show()
except BaseException as e:
print(str(e))
def update_message(self):
self.val += 1
self.msg += '.'
if self.val < 20:
self.info_label.setText(self.msg)
else:
self.val = 0
self.msg = self.o_msg
QtWidgets.QApplication.processEvents()
def main():
app = QtWidgets.QApplication(sys.argv) # A new instance of QApplication
form = InfoMessage('Loading ') # We set the form to be our MainWindow (design)
app.exec_() # and execute the app
if __name__ == '__main__': # if we're running file directly and not importing it
main() # run the main function
When I run the InfoMessage function alone it works fine and it updates every 0.5 seconds etc. However, when I fun this as part of the loading file the GUI is blank and incorrectly displayed. I know it is staying in the test function because of the print statement in there.
Can someone point me in the right direction? I think I am missing a couple of steps.
First, there are two ways of doing this. One way is to use the Python builtin threading module. The other way is to use the QThread library which is much more integrated with PyQT. Normally, I would recommend using QThread to do threading in PyQt. But QThread is only needed when there is any interaction with PyQt.
Second, I've removed processEvents() from InfoMessage because it does not serve any purpose in your particular case.
Finally, setting your thread as daemon implies your thread will never stop. This is not the case for most functions.
import sys
import threading
import time
from PyQt5 import uic, QtCore, QtWidgets
from PyQt5.QtCore import QThread
def long_task(limit=None, callback=None):
"""
Any long running task that does not interact with the GUI.
For instance, external libraries, opening files etc..
"""
for i in range(limit):
time.sleep(1)
print(i)
if callback is not None:
callback.loading_stop()
class LongRunning(QThread):
"""
This class is not required if you're using the builtin
version of threading.
"""
def __init__(self, limit):
super().__init__()
self.limit = limit
def run(self):
"""This overrides a default run function."""
long_task(self.limit)
class InfoMessage(QtWidgets.QDialog):
def __init__(self, msg='Loading ', parent=None):
super(InfoMessage, self).__init__(parent)
uic.loadUi('loading.ui', self)
# Initialize Values
self.o_msg = msg
self.msg = msg
self.val = 0
self.info_label.setText(msg)
self.show()
self.timer = QtCore.QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.update_message)
self.timer.start()
def update_message(self):
self.val += 1
self.msg += '.'
if self.val < 20:
self.info_label.setText(self.msg)
else:
self.val = 0
self.msg = self.o_msg
def loading_stop(self):
self.timer.stop()
self.info_label.setText("Done")
class MainDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
# QThread Version - Safe to use
self.my_thread = LongRunning(limit=10)
self.my_thread.start()
self.my_loader = InfoMessage('Loading ')
self.my_thread.finished.connect(self.my_loader.loading_stop)
# Builtin Threading - Blocking - Do not use
# self.my_thread = threading.Thread(
# target=long_task,
# kwargs={'limit': 10}
# )
# self.my_thread.start()
# self.my_loader = InfoMessage('Loading ')
# self.my_thread.join() # Code blocks here
# self.my_loader.loading_stop()
# Builtin Threading - Callback - Use with caution
# self.my_loader = InfoMessage('Loading ')
# self.my_thread = threading.Thread(
# target=long_task,
# kwargs={'limit': 10,
# 'callback': self.my_loader}
# )
# self.my_thread.start()
def main():
app = QtWidgets.QApplication(sys.argv)
dialog = MainDialog()
app.exec_()
if __name__ == '__main__':
main()
Feel free to ask any follow up questions regarding this code.
Good Luck.
Edit:
Updated to show how to run code on thread completion. Notice the new parameter added to long_task function.
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?
I'm trying to make basic functionality
after pressing "start" button start counter , after pressing stop button stop counter,
but after I start process, it looks like only counting thread is working and it's not possible to press stop button
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
from test.test_sax import start
import time
from threading import Thread
import threading
class Example(QtGui.QWidget):
x = 1
bol = True
def __init__(self):
super(Example, self).__init__()
self.qbtn = QtGui.QPushButton('Quit', self)
self.qbtn.resize(self.qbtn.sizeHint())
self.qbtn.move(50, 50)
self.qbtn2 = QtGui.QPushButton('Start', self)
self.qbtn2.resize(self.qbtn2.sizeHint())
self.qbtn2.move(150, 50)
self.qbtn.clicked.connect(self.stopCounter)
self.qbtn2.clicked.connect(self.startUI)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Quit button')
self.show()
def stopCounter(self):
Example.bol = False
def startUI(self):
Example.bol = True
thread = Thread(self.counterr())
def counterr(self):
x = 0
while Example.bol:
print x
x += 1
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
a = Example()
sys.exit(app.exec_())
thx
Now you call the slow function before you even create the thread. Try this instead:
thread = Thread(target=self.counterr)
thread.start()
In a Qt application you might also consider the QThread class that can run its own event loop and communicate with your main thread using signals and slots.
You are using the Thread class completely incorrectly, I'm afraid. You are passing it the result of the counterr method, which never returns.
Pass counterr (without calling it) to the Thread class as the target, then start it explicitly:
def startUI(self):
self.bol = True
thread = Thread(target=self.counterr)
thread.start()
Also, just access bol as an instance variable, not a class variable.