Python Forum
[Tkinter] Code question - 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: [Tkinter] Code question (/thread-11136.html)



Code question - robgar2001 - Jun-24-2018

Hello I am a c# coder and I am learning Python so be kind please.
I just can't figure out why this code isn't working.
My intensions are to let a screen appear and when I push a button a thread gets started.
When I push another button a variable gets changed so that a led starts blinking on my raspberry pi.
import tkinter as tk
from time import sleep
import threading
from gpiozero import LED

blink=False
led=LED(14)

screen = tk.Tk()
screen.title("Control panel")
#screen.attributes('-fullscreen', True)

def click():
    global blink
    if blink==True:
        blink=False
    else:
        blink=True

yaw = tk.Button(screen,text="Click for blink loop",command=click())
yaw.pack()
screen.mainloop()
def printer():
    global blink
    while 1:
        led.on()
        sleep(2)
        led.off()
        sleep(2)
        
th = threading.Thread(printer())
threads.append(th)
th.start()



       



RE: Code question - Barrowman - Jun-24-2018

So what does happen?
Does the LED blink? Does anything happen?
Don't forget that some of this will only work on a Pi as most here won't have a Pi to try it on anyway.


RE: Code question - wuf - Jun-24-2018

Hi robgar2001

This should work better:
import tkinter as tk
from gpiozero import LED

led = LED(14)

def blink_led(blink=True):
    if blink:
        led.on()
    else:
        led.off()
    blink=not blink
    screen.after(2000, blink_led, blink)
    
screen = tk.Tk()
screen.title("Control panel")

tk.Button(screen,text="Click for blink loop",command=blink_led).pack()

screen.mainloop()
wuf ;-)