Python3 uses tkinter to implement a simple ui instance

Source: Internet
Author: User


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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.