Prof101
Prof101

Reputation: 59

Python: Tkinter pausing a while loop

So i'm trying to create a program that displays the cpu usage of your computer in real time. I wanted to use tkinter to add some buttons to the program. I decided to add a stop button that closes the program and a pause button that stops the cpu usage from updating. I was able to create the stop button but when I started the pause button I realized that I was unable to pause the while loop I using. Does anyone know what i'm doing wrong?

from tkinter import *
import psutil
from time import sleep

root = Tk()
var = StringVar()

label = Label( root, textvariable=var, relief=GROOVE, height=3, width=6, bd=4)
f = Frame(root, height=100, width=180)
f.pack_propagate(0)
f.pack()
stop=0
def pause1():
    stop=1
    print('It works')
def findcpu():
    if stop==0:
        root.update()
        sleep(0.001)
        cpu = psutil.cpu_percent(interval=1, percpu=False)
        var.set(cpu)
        label.pack()
        root.update()
    elif stop==1:
        print('It really works')
        loop=1 

class pauseButton(Button):
    def __init__(self, parent):
        Button.__init__(self, parent)
        self['text']= 'pause'
        self['bg']='orange'
        self['height']=3
        self['width']=6
        self['bd']=4
        self['relief']=GROOVE
        self.button=Button(self)
        self['command']=pause1        
        self.pack(side=LEFT)   
class quitButton(Button):
    def __init__(self, parent):
        Button.__init__(self, parent)
        self['text'] = 'End'
        self['bg']='red'
        self['height']=3
        self['width']=6
        self['bd']=4
        self['padx']=0
        self['pady']=0
        self['relief']=GROOVE
        self['activebackground']='brown'
        self.button = Button(self)
        self['command'] = parent.destroy
        self.pack(side=RIGHT)
quitButton(root)
pauseButton(root)
loop=0
stop=0
num1=0
num2=1

while loop==0:
    findcpu()


root.mainloop()
print('Done')

Upvotes: 0

Views: 1575

Answers (2)

user1599304
user1599304

Reputation:

The scenario is I create a pop-up GUI – OK button

Then I want to wait on the user to click OK to continue

Problem: I create a loop in python alone this “locks out” the GUI part from updating.

In fact the even though I instruct Tk to create a Button pop-up, it never pops-up.

The solution I found is to

1) force-refresh the TK using “update” while (self.wait_for_operator.get()):

                   root.update() 
                   root.update_idletasks()

2) I use a special TK variable called an IntVar

“There’s no way to track changes to Python variables, but Tkinter allows you to create variable wrappers that can be used wherever Tk can use a traced Tcl variable.”

This value is set when the user presses the the OK button

The combinations of 1) and 2) means I am successfully able to create a user-controlled loop from a GUI.


The relevant code sections are

self.wait_for_operator.set(1)    

self.button = Button(self.top, text="OK", command= self._handle_start_key_and_serial_input)
self.button.pack()

while (self.wait_for_operator.get()):

   root.update() 
   root.update_idletasks()

def _handle_start_key_and_serial_input(self):
  self.wait_for_operator.set(0)        
  self.top.destroy()  

Upvotes: 0

Skycc
Skycc

Reputation: 3555

You are using variable stop and loop as global but you are modifying it as local variable, thus global copy keep unchange as initialize value. Just need to explicitly declare variable loop and stop as global to indicate you wanna access the global copy instead because it will default to local scope if not being specified

Add line below to both pause1 and findcpu function

global stop, loop

Upvotes: 1

Related Questions