您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/6/7 1:55:53发布19次查看
代码如下:
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()
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product