0

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')
4
  • At top of both pause1 and findcpu function, try add global stop, loop Commented Oct 30, 2016 at 1:42
  • Thank you so much. This is exactly what I needed. Thank you. Commented Oct 30, 2016 at 1:46
  • Glad that it solve your problem, i add a bit explanation hope it helps :) Commented Oct 30, 2016 at 1:52
  • Remember that if any of the answers solved your question, is good that you mark it as accepted (big checkbox to the left of the answer). It'll give you reputation points, it'll give the person that spent time answering points and most importantly, it'll help future readers see that the answer was helpful. See meta.stackexchange.com/questions/5234/… Commented Oct 30, 2016 at 1:53

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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()  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.