python3使用tkinter實(shí)現(xiàn)ui界面簡單實(shí)例
更新時(shí)間:2014年01月10日 10:33:04 作者:
使用tkinter創(chuàng)建一個(gè)小窗口,布置2個(gè)按鈕,一個(gè)btn關(guān)閉窗口,另一個(gè)btn用于切換執(zhí)行傳入的2個(gè)函數(shù),簡單的小代碼,大家參考使用吧
復(fù)制代碼 代碼如下:
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='關(guān)閉窗口', 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'] = '啟動服務(wù)'
self.stat = False
self.root.iconbitmap(self.stoIco)
else:
if self.staFunc():
self.btnSer['text'] = '停止服務(wù)'
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()
相關(guān)文章
python實(shí)現(xiàn)添加圖片到word文檔中
這篇文章主要介紹了python實(shí)現(xiàn)添加圖片到word文檔中方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Python3.7在anaconda里面使用IDLE編譯器的步驟詳解
這篇文章主要介紹了Python3.7在anaconda里面使用IDLE編譯器的步驟,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-04-04

解決win10 vscode 無法激活python 虛擬環(huán)境的問題
這篇文章主要介紹了win10 vscode 無法激活python 虛擬環(huán)境的解決辦法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
2021-10-10