当前位置: 移动技术网 > IT编程>脚本编程>Python > python3使用tkinter实现ui界面简单实例

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

2019年06月14日  | 移动技术网IT编程  | 我要评论

叶剑英之子坐轮椅现身,卧龙吟征收事件,杀死指挥官


复制代码 代码如下:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网