Chappers89
Chappers89

Reputation: 21

Tkinter button only works once

Tkinter button only runs a separate script once

Hello all, Im a new to python and raspberry pi and have been searching high and low on how to get a Tkinter button to run a script more than once on my raspberry pi. From research I believe it has something to do with name="main", but I cant figure out what needs to be done and why. My button runs a separate python file (called SendRF.py) in the same directory that generates an RF signal, it works the first time but then the button click does nothing else after. Any advice would be much appreciated :)

from tkinter import *

#create a window
window =Tk()
window.title("Chappers Home Automation project")

#define a function
def test_function ():
    import SendRF

#create a button
B = Button(text ="Test Button 1", command=test_function)

B.pack(padx = 100, pady = 50)
window.mainloop()

No error messages appear. The button sends the RF signal when pressed the first time, but nothing happens for further button clicks.

Upvotes: 2

Views: 3484

Answers (3)

Anuj
Anuj

Reputation: 23

You can check if the function is working correctly by adding a simple print statement inside your function

from tkinter import *

#create a window
window =Tk()
window.title("Chappers Home Automation project")

#define a function
def test_function ():
    import SendRF
    print('CHECK') 

#create a button
B = Button(text ="Test Button 1", command=test_function)

B.pack(padx = 100, pady = 50)
window.mainloop()

Upvotes: 1

Bogdan Prisacaru
Bogdan Prisacaru

Reputation: 1

It is working one time, cause you alredy have the SendRf function mported, you need to close it, after to import again

Upvotes: 0

HSchmale
HSchmale

Reputation: 1929

You can't import a module multiple times. Each additional import for the same module is a NOP. You need to functionize whatever is in sendRF, and call that function in test_function.

Upvotes: 1

Related Questions