I have a tkinter program (Python) That contains a button with a PNG image on it (image 1). (Just upgrading button styles) When you click the button it will run a function (with some code) and it also changes the button image (image 2) (to have other color and text). But when I click it, it displays the image that I wanted (image 2) for one millisecond and then it vanishes the button image and I'm unable to interact with the button again. (The button gets completely blank)
I want to it successfully change the button image (from image 1 to 2) and when I click it again it would change the image to the previous one (from image 2 to 1).
def openServer():
global state, start, process, stateD
if state == 0:
state = 1
#stateD.config(text="Opening...",fg="orange")
stateD.config(text="Online", fg="green")
#start.config(text="Stop")
btn2 = tk.PhotoImage(file='../../../icons/button_stop.png')
start.config(image=btn2)
executable = '*HIDDEN*'
if process is None:
Timer(1.0, consoleTimer).start()
process = subprocess.Popen(executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
print("Server started.")
elif state == 1:
stateD.config(text="Offline",fg="black")
#start.config(text="Start")
btn = tk.PhotoImage(file='../../../icons/button_start.png')
start.config(image=btn)
try:
process.stdin.write("exit".encode())
process.stdin.flush()
except:
state = 0
process = None
state = 0
btn= tk.PhotoImage(file='../../../icons/button_start.png')
start = tk.Button(server,borderwidth=0, text= "Start",image=btn,width= 1500,command=lambda:Timer(0,openServer).start())
start.pack()
Note: the window is 1500x800 the button width is 1500px and the button_image is 1500px