-1

I am trying to create a simple GUI using Tkinter. I created a label with a text. This text should be changed when a button (physical) is pressed. I read about using the after() method, but it only runs a method ones, it should always check for user input.

while 1:
    root = tk.Tk()
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    input_state = GPIO.input(21)

    menutxt = tk.StringVar()
    menuLabel = tk.Label(root, textvariable=menutxt).pack()

    if input_state == False:
        menutxt.set("TEXT2")
    else:
        menutxt.set("TEXT1")
    root.mainloop()
Enes
  • 1
  • 1
  • You can't use `.Tk() ... .mianloop()` inside a loop. Read [How do you run your own code alongside Tkinter's event loop?](https://stackoverflow.com/a/459131/7414759) and follow that pattern. – stovfl Jan 03 '19 at 10:47
  • the code above already is in it's own process/thread. I am very new to this and don't know how to get that working on my code. Could you help me out please? – Enes Jan 03 '19 at 11:15
  • There are couple problems here. It's to broad for SO, setup a [GitHubGist](https://gist.github.com), i will comment there. – stovfl Jan 03 '19 at 14:29

1 Answers1

-2

You don't know what is a loop ? you can try with

root = tk.Tk()
menutxt = tk.StringVar()
menuLabel = tk.Label(root, textvariable=menutxt).pack()
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while 1:
    input_state = GPIO.input(21)
    if input_state == False:
        menutxt.set("TEXT2")
    else:
        menutxt.set("TEXT1")
root.mainloop()
patel
  • 430
  • 1
  • 4
  • 9