Python Forum
[Tkinter] Code question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Code question
#1
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()



       
Reply
#2
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.
Reply
#3
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 ;-)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020