Python3 uses Tkinter to implement a simple instance of the UI interface _python

Source: Internet
Author: User


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

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.