欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python3使用tkinter实现ui界面简单实例

程序员文章站 2023-11-16 16:05:34
复制代码 代码如下:import timeimport tkinter as tkclass window:    def __init_...


python3使用tkinter实现ui界面简单实例

复制代码 代码如下:

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='关闭窗口', 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'] = '启动服务'
                self.stat = false
                self.root.iconbitmap(self.stoico)
        else:
            if self.stafunc():
                self.btnser['text'] = '停止服务'
                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()