My GUI holds 8 Buttons. Each Button click calls the event handler which then forwards to another function. All of these actions take about 4 seconds. My problem is that this causes the Button to stay in SUNKEN state while the event is handled and also causes the other Buttons to be non-responsive.
What I would like is to release the Button from the SUNKEN state immediately after the click and continue the event handling in the background.
How can this be solved ? Is there a way to make the button released before the event handler finished its job ?
After Editing:
Here is my code:
from tkinter import Tk, Menu, Button
import telnetlib
from time import sleep
On_Color = '#53d411'
Off_Color = '#e78191'
def Power_On_Off (PowerSwitchPort, Action):
'''
Control the Power On Off Switch
'''
on_off = telnetlib.Telnet("10.0.5.9", 2016)
if Action == 'On' or Action =='ON' or Action == 'on':
StringDict = {'1': b"S00D1DDDDDDDE", '2': b"S00DD1DDDDDDE", '3': b"S00DDD1DDDDDE", '4': b"S00DDDD1DDDDE",
'5': b"S00DDDDD1DDDE", '6': b"S00DDDDDD1DDE", '7': b"S00DDDDDDD1DE", '8': b"S00DDDDDDDD1E"}
elif Action == 'Off' or Action =='OFF' or Action == 'off':
StringDict = {'1': b"S00D0DDDDDDDE", '2': b"S00DD0DDDDDDE", '3': b"S00DDD0DDDDDE", '4': b"S00DDDD0DDDDE",
'5': b"S00DDDDD0DDDE", '6': b"S00DDDDDD0DDE", '7': b"S00DDDDDDD0DE", '8': b"S00DDDDDDDD0E"}
PowerSwitchPort = str(PowerSwitchPort)
on_off.read_eager()
on_off.write(b"S00QLE\n")
sleep(4)
on_off.write(StringDict[PowerSwitchPort])
on_off.close()
def OnButtonClick(button_id):
if button_id == 1:
# What to do if power_socket1 was clicked
Power_On_Off('1', 'Off')
elif button_id == 2:
# What to do if power_socket2 was clicked
Power_On_Off('1', 'On')
def main ():
root = Tk()
root.title("Power Supply Control") #handling the application's Window title
root.iconbitmap(r'c:\Users\alpha_2.PL\Desktop\Power.ico') # Handling the application icon
power_socket1 = Button(root, text = 'Socket 1 Off', command=lambda: OnButtonClick(1), bg = On_Color)
power_socket1.pack()
power_socket2 = Button(root, text = 'Socket 1 On', command=lambda: OnButtonClick(2), bg = On_Color)
power_socket2.pack()
'''
Menu Bar
'''
menubar = Menu(root)
file = Menu(menubar, tearoff = 0) # tearoff = 0 is required in order to cancel the dashed line in the menu
file.add_command(label='Settings')
menubar.add_cascade(label='Options', menu = file)
root.config(menu=menubar)
root.mainloop()
if __name__ == '__main__':
main()
As can be seen, I create 2 Buttons one Turns a switch On and the other turns it Off. On/Off actions are delayed in about 4 seconds. This is only small part of my Application that I took for example. In my original code I use a Class in order to create the GUI and control it