Copy codeThe Code is as follows:
Import time
Import tkinter as tk
Class Window:
Def _ init _ (self, title = 'nms', width = 300, height = 120, staFunc = bool, stoFunc = bool ):
Self. w = width
Self. h = height
Self. stat = True
Self. staFunc = staFunc
Self. stoFunc = stoFunc
Self. staIco = None
Self. stoIco = None
Self. root = tk. Tk (className = title)
Def center (self ):
Ws = self. root. winfo_screenwidth ()
Hs = self. root. winfo_screenheight ()
X = int (ws/2)-(self. w/2 ))
Y = int (hs/2)-(self. h/2 ))
Self. root. geometry ('{}x {}+ {}'. format (self. w, self. h, x, y ))
Def packBtn (self ):
Self. btnSer = tk. Button (self. root, command = self. event, width = 15, height = 3)
Self. btnSer. pack (padx = 20, side = 'left ')
BtnQuit = tk. Button (self. root, text = 'close Windows', command = self. root. quit, width = 15, height = 3)
BtnQuit. pack (padx = 20, side = 'right ')
Def event (self ):
Self. btnSer ['state'] = 'Disabled'
If self. stat:
If self. stoFunc ():
Self. btnSer ['text'] = 'start Service'
Self. stat = False
Self. root. iconbitmap (self. stoIco)
Else:
If self. staFunc ():
Self. btnSer ['text'] = 'Stop Service'
Self. stat = True
Self. root. iconbitmap (self. staIco)
Self. btnSer ['state'] = 'active'
Def loop (self ):
Self. root. resizable (False, False) # disable window size Modification
Self. packBtn ()
Self. center () # center window
Self. event ()
Self. root. mainloop ()
######################################## ################################
Def sta ():
Print ('Start .')
Return True
Def sto ():
Print ('Stop .')
Return True
If _ name _ = '_ main __':
Import sys, OS
W = Window (staFunc = sta, stoFunc = sto)
W. staIco = OS .path.join(sys.exe c_prefix, 'dlls \ pyc. ico ')
W. stoIco = OS .path.join(sys.exe c_prefix, 'dlls \ py. ico ')
W. loop ()