-1

So Im running everything on threads and the run and not run works as intended but running doesnt print, I tried calling it in

        status["text"]="Working"
        print("run")
        process()

but that just freezes up my whole program, I also tried putting in root.after on the end but still freezes everything up. Any way here is the snippet of the part im talking about.

def keyStart():
    global run
    run = True
    while run == True:
        status["text"]="Working"
        print("run")
        process()



def keyStop():
    global run
    run = False
    if run == False:
        status["text"]="Not Working"
        print("not run")

        
buttonStart = Button(root, text="Start", command=keyStart)
buttonStart.grid(columnspan=1, column=0, row=3)
buttonStop = Button(root, text="Stop", command=keyStop)
buttonStop.grid(columnspan=1, column=1, row=3)



            
def process():
    global run
    while run == True:
        print("running")
That Guy
  • 55
  • 1
  • 6

1 Answers1

0

Tkinter, like all GUI frameworks, is event driven. Your GUI calls don't actually do any drawing. They just sent messages. There is a main loop (root.mainloop) that fetches and dispatches messages for processing. As long as a message handler is running, it can't get back to the main loop, and no more messages will be handled.

That's the issue here. Your button press will trigger your keystart, which calls autofisher, and autofisher never returns. It runs a 100% tight CPU loop. Because you are not getting back to the main loop, the GUI is frozen. No more activity will be handled.

You shouldn't be mixing tkinter and keyboard. tkinter has its own keyboard handling ability that plays nicely with the rest of the framework. You need to throw out the keyboard module, and use tkinter bind calls to associate callbacks with keystrokes.

https://python-course.eu/tkinter/events-and-binds-in-tkinter.php https://www.pythontutorial.net/tkinter/tkinter-event-binding/

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • But I dont understand why the loops dont run alongside eachother, isnt that what the threading is for? – That Guy Mar 20 '22 at 02:50
  • Also is there a way to run autofisher then when keyStart is run? – That Guy Mar 20 '22 at 02:52
  • `keyStart` and `keyStop` are not threads. They are callbacks, always called in the main thread. And since `autofisher` is called from `keyStart`, it also is not running in a thread. This is just a case where you have chosen the wrong tool. You need to work within the GUI framework. – Tim Roberts Mar 20 '22 at 02:53
  • I've not objected before, but what's the point of that? That's going to print "running" to the console thousands of times per second, uselessly. What are you really trying to do here? – Tim Roberts Mar 20 '22 at 02:54
  • Threading is a useful tool, but Python threading has some serious limitations. Python has a "Global Interpreter Lock", which means that only one thread at a time can be executing Python code. If you have threads doing I/O that spend lots of time waiting, it works great. If you have tight CPU loops, like you have, it doesn't help at all. – Tim Roberts Mar 20 '22 at 02:56
  • ```running``` was just a way for me to make sure the loop ran, my actual goal is to have the code underneath do its thing until its stopped(which is detect pixels on the screen and press button on the correct time.) – That Guy Mar 20 '22 at 02:56
  • You can have `keyStart` run `autofisher` in a thread (so, do `threading.Thread(target=autofisher).start()`, but you won't like it. ;) – Tim Roberts Mar 20 '22 at 02:57
  • Im not gonna lie this is kind of discouraging, I came in excited, getting stuck and figuring things out was good and I learned but as soon as I try to implement tkinter, lol everything is falling apart. – That Guy Mar 20 '22 at 02:58
  • Well, you can't get discouraged. There are RULES to follow to make applications behave in a consistent way and play well with others. You just need to learn the framework rules so you can work within them. You're writing a GUI application here. There are a lot of moving pieces.. – Tim Roberts Mar 20 '22 at 03:05
  • The ```threading.Thread(target=autofisher).start()``` literally worked, why wouldnt I like it, is it bad for performance or something? – That Guy Mar 20 '22 at 03:07
  • Yes, because it is literally wasting 100% of a CPU and flooding your terminal with thousands of useless messages. – Tim Roberts Mar 20 '22 at 03:48