This question is a follow up to:
Run long process continously using Tkinter (Python 2.7)
In the previous topic i asked about running a funtion continuously until a button is pressed. This funtion takes a long time to complete, and basically i wanted to stop the process (don't repeat the function) if a button was pressed.
I got an answer to my question, but i was wondering if this is possible using the Checkbutton function.
I know i can call a function using the Checkbutton (using command=[funtionname]), but what i need is a way to call a function continuously while the Checkbutton is checked, and stop when it's unchecked. Is this possible using tkinter?
Thanks in advance for any answers,
Harm
You mentioned that the process is a repeated process. You can just check the state of the button at every repeat with var.get() (assuming that the var is the variable of the checkbutton) and break out of the function if it is unchecked.
This is not a really elegant solution, and if a repeat takes a long time, the last "round" will still finish after the uncheck, so it is not immediate.
Related
I have a question about the window.Update and window.read in PySimpleGui.
I see that when I use window.read, the programm didn't continue while i did not click on a button. But if I click on a button the window will do all the Update that i want until it will meet a new window.read or window.refresh.
My problem is that i want to do an window.update, stop during a few second, and do another winodw.update after. I try to use time.sleep but it didn't work it seems like all the line of programm is execute in same time.
So i want to know if it is possible to stop the programm during a few second between two window.read or if it is possible to "force" the window.read or window.refresh (skip them without click on a button).
Thank you in advance and sorry for my english who is probably bad...
I would like to ask how could I add dynamically some widgets in my application one by one and not all at once. Those widgets are added in a for loop which contains the add_widget() command, and is triggered by a button.
So I would like to know if there is a way for the output to be shown gradually, and not all at once, in the end of the execution. Initially I tried to add a delay inside the for loop, but I'm afraid it has to do with the way the output is built each time.
EDIT: Well, it seems that I hadn't understood well the use of Clock.schedule_interval and Clock.schedule_once, so what I had tried with them (or with time.sleep) didn't succeed at all. But obviously, this was the solution to my problem.
Use Clock.schedule_interval or Clock.schedule_once to schedule each iteration of the loop at your desired time spacing.
I'm learning how to program in Python and use the PIL (Pillow) in order to create small bots which can recognize changes in the screen in order to execute repetitive commands (keyboard presses with pynput module).
I did a small GUI with Tkinter in order to get user information regarding the parameters which the user is going to use (Entry widgets) at their use of the bot and I also need a few buttons for enabling/disabling certain parts of the code (i'm using check buttons and using boolean logic in order to enable/disable) and also for enabling the main function of the bot (which is to press a button once it recognizes a certain change at the screen).
What I am currently experiencing is that the once I run my program, it opens up my GUI and all that a user would see but it doesn't execute my main function, it only executes the main function AFTER I close the GUI.
I'd love to post my code but it exceeds 220 lines and it would probably become even more confusing.
So, long story short, is it a kind of error which happens a lot when inexperienced programmers try to create softwares with tkinter? If yes, what causes it and how can it be fixed?
I appreciate any help you guys can get me. :]
...creates frames, widgets, entries...
mainHealerVar = IntVar()
main_healer_check = Checkbutton(root, text="Enable the bot", variable=mainHealerVar) #this should mean that when i press this checkbutton it'll enable the bot and perform the repetitive commands for as long as it stays checked
... some more buttons/code...
root.mainloop() #right after I stop setting up the GUI, I place this function
... make logic in order to know which part of the repetitive functions to execute...
if mainHealerVar.get() == 1:
mainHealFunction() #function to check if the checkbutton is still pressed (if it is, it should execute mainHealFunction()
It was supposed to execute the repetitive commands by the pynput module as soon as i clicked on the enabling Check-button, but it doesn't execute anything until I close the GUI, then it starts to execute the repetitive commands. I have also tried using a while loop instead of an "if" at the end, but ended up with the same result.
I understand that in tkinter once mainloop() has been run, no code after it will run until the window has been destroyed. I have found that the common solution is to use tk.after to call a function repeatedly at certain intervals. However I am using a for loop, and every time it loops it updates a variable which I want to see change in the GUI.
PB=myProgressBar()
for i in range (0, len(dataset)):
performfunction
PB.update(i)
PB.quit()
The aim is while performing an operation on each item in the dataset, the GUI will show how far the program is.
Within my progress bar class I have tried using a tk.IntVar as my ttk.ProgressBar value and setting the value through the PB.update(i) to update it.
I have also tried using ProgressBar['value']=i in my update method of the progress bar class.
In both cases if i run mainloop() before the loop, the for loop doesn't run (as you'd expect) but I'm not sure how to run mainloop and get update the GUI without a messy tk.after function that would probably have to involve a self.i value (and then do self.i+=1 at the end of the function that replaces the loop).
Is there a clean 'pythonic' way to do this?
I am writing a timer program in Python using PyGTK. It is precise to the hundredths place. Right now, I am using a constantly updated label. This is a problem, because if I resize the window while the timer is running, Pango more often than not throws some crazy error and my program terminates. It's not always the same error, but different ones that I assume are some form of failed draw. Also, the label updates slower and slower as I increase the font size.
So, I am wondering if there is a more correct way to display the timer. Is there a more stable method than constantly updating a label?
Updating a label should work perfectly reliably, so I suspect you're doing something else wrong. Are you using threads? What does your code look like? How small can you condense your program (by removing functionality, not by obfuscating the code), without making the problem go away?
I figured out the problem. It was indeed a problem with the threads. I never would've guessed that myself. The trick is to use gobject.timeout_add() to create a timer instead of a threaded loop. Here is some information about gobject.timeout_add():
http://faq.pygtk.org/index.py?req=show&file=faq01.021.htp
Don't forget to have your function return True, or the timer will stop.