Copy Code code 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 window ', 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) #禁止修改窗口大小
SELF.PACKBTN ()
Self.center () #窗口居中
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.exec_prefix, ' Dlls\pyc.ico ')
W.stoico = Os.path.join (Sys.exec_prefix, ' Dlls\py.ico ')
W.loop ()