Python Forum
while loop with gui not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: while loop with gui not working (/thread-24855.html)



while loop with gui not working - pythonbegginer - Mar-07-2020

hi, I am trying to make a program continuously print on off if a radio button is selected and if another radio button is selected, I want it to stop. here is my code.

from time import sleep
from tkinter import *
from tkinter import ttk

root = Tk()
class LED:
    def __init__(self, ON, OFF, delay):
        self.ON = ON
        self.OFF = OFF
        self.delay = delay

    def LED_blink(self):
        print(self.ON)
        sleep(self.delay)
        print(self.OFF)
        sleep(self.delay)

label = ttk.Label(root, text='ON')
label.pack()

def ONOFF():
    while var == 1:
        Led.LED_blink()

def ONOFFSTOP():
    while var1 == 1:
        break

Led = LED('ON', "OFF", 1)

var = IntVar()
var1 = IntVar()
radio1 = ttk.Radiobutton(root, text = 'Start', variable = var, command = ONOFF, value = 1).pack()
radio2 = ttk.Radiobutton(root, text = 'Stop', variable = var1, command = ONOFFSTOP, value = 1).pack()

root.mainloop()
any help appreciated.


while loop with gui not working - pythonbegginer - Mar-07-2020

hi, I tried the classes by themselves and they operated fine, but now I want to add a GUI and a while loop to my code with 2 radio buttons. here is my code.

from time import sleep
from tkinter import *
from tkinter import ttk

root = Tk()
class LED:
    def __init__(self, ON, OFF, delay):
        self.ON = ON
        self.OFF = OFF
        self.delay = delay

    def LED_blink(self):
        print(self.ON)
        sleep(self.delay)
        print(self.OFF)
        sleep(self.delay)

label = ttk.Label(root, text='ON')
label.pack()

def ONOFF():
    while var == 1:
        Led.LED_blink()

def ONOFFSTOP():
    while var1 == 1:
        break

Led = LED('ON', "OFF", 1)

var = IntVar()
var1 = IntVar()
radio1 = ttk.Radiobutton(root, text = 'Start', variable = var, command = ONOFF, value = 1).pack()
radio2 = ttk.Radiobutton(root, text = 'Stop', variable = var1, command = ONOFFSTOP, value = 1).pack()

root.mainloop()
whenever I press the start button it does nothing and stays pressed. help appreciated.


RE: while loop with gui not working - Larz60+ - Mar-07-2020

Quote:
def ONOFF():
    while var == 1:
        Led.LED_blink()

Where do you expect this might terminate?
what is the value of var after first loop?