一、窗体
1.1窗体居中
from tkinter import *
class mainWindow():
def __init__(self):
self.mainwin = Tk()
method.tk_size(win=self.mainwin, ww=400, wh=600, xr=2, yr=2, t='首页')
self.mainwin.mainloop()
class method():
#设置窗体
def tk_size(win,t,ww,wh,xr,yr):
win.title(t)
sw = win.winfo_screenwidth()
sh = win.winfo_screenheight()
x = (sw - ww) / xr
y = (sh - wh) / yr
win.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
win.resizable(0, 0)
win.resizable(0, 0)
mainWindow()
1.2多选框
from tkinter import *
class mainWindow():
def __init__(self):
self.mainwin = Tk()
method.tk_size(win=self.mainwin, ww=400, wh=600, xr=2, yr=2, t='抓取日志工具--V1.0')
v1 = IntVar()
v2 = IntVar()
v3 = IntVar()
v4 = IntVar()
Chkeckbottn1 = Checkbutton(self.mainwin, text='一', variable=v1, onvalue=1, offvalue=0)
Chkeckbottn1.place(x=110, y=66, width=45, height=30, )
Chkeckbottn2 = Checkbutton(self.mainwin, text='二', variable=v2, onvalue=1, offvalue=0)
Chkeckbottn2.place(x=170, y=66, width=45, height=30)
Chkeckbottn3 = Checkbutton(self.mainwin, text='三', variable=v3, onvalue=1, offvalue=0)
Chkeckbottn3.place(x=225, y=66, width=45, height=30)
Chkeckbottn4 = Checkbutton(self.mainwin, text='ALL', variable=v4, onvalue=1, offvalue=0, command=lambda: method.tk_SelectAll(s=v4.get()))
Chkeckbottn4.place(x=275, y=66, width=45, height=30)
self.mainwin.mainloop()
class method():
#设置窗体
def tk_size(win,t,ww,wh,xr,yr):
win.title(t)
sw = win.winfo_screenwidth()
sh = win.winfo_screenheight()
x = (sw - ww) / xr
y = (sh - wh) / yr
win.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
win.resizable(0, 0)
win.resizable(0, 0)
#多选框设置全选
def tk_SelectAll(s):
if s == 1:
Chkeckbottn1.select()
Chkeckbottn2.select()
Chkeckbottn3.select()
Chkeckbottn4.select()
elif s == 0:
Chkeckbottn1.deselect()
Chkeckbottn2.deselect()
Chkeckbottn3.deselect()
1.3IP格式判断
def tk_Check_Ip(self):
#获取输入框IP
v = EntryMSCIP.get()
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(v):
textinfo.insert(INSERT, '--->正确格式的IP<---\n', "t1")
return True
else:
textinfo.insert(INSERT, '--->不正确格式的IP<---\n', "t2")
return False
1.4Text文本框
from tkinter import *
class mainWindow():
def __init__(self):
self.mainwin = Tk()
method.tk_size(win=self.mainwin, ww=400, wh=600, xr=2, yr=2, t='抓取日志工具--V1.0')
textinfo = Text(width=53, height=30)
textinfo.place(x=10, y=180)
#按钮
Button(self.mainwin, text="提交", width=140, height=60, command:self.commit).place(x=60, y=118, width=61, height=30)
#设置文本颜色
textinfo.tag_config("t3", backgroun="moccasin")
textinfo.tag_config("t2", backgroun="lightcoral")
textinfo.tag_config("t1", backgroun="lightgreen")
#文本禁止选项
# self.Text.config(cursor="none") #隐藏鼠标
# self.Text.bind('<KeyPress>', lambda e: 'break') #禁止键盘输入
# self.Text.bind('<Button-1>', lambda e: 'break') # 禁止鼠标左键
# self.Text.bind('<B1-Motion>', lambda e: 'break') # 禁止鼠标左键移动
# self.Text.bind('<Double-Button-1>', lambda e: 'break') # 禁止双击鼠标左键
self.mainwin.mainloop()
def commit(self):
textinfo.insert(INSERT, '--->插入的文本,亮绿色<---\n', "t1")
mainWindow()
1.5选择文件路径
from tkinter.filedialog import askdirectory
#设置文件路径
def tk_SelectPath(self):
global path_
path_ = askdirectory() # 使用askdirectory()方法返回文件夹的路径
if path_ == "":
pass
else:
path_ = path_.replace("/", "\\") # 实际在代码中执行的路径为“\“ 所以替换一下
EntryPath.delete(0, END)
EntryPath.insert(INSERT, path_)
1.6命令运行过程较长,窗体无响应
from tkinter import *
import threading
class mainWindow():
def __init__(self):
self.mainwin = Tk()
method.tk_size(win=self.mainwin, ww=400, wh=600, xr=2, yr=2, t='抓取日志工具--V1.0')
Button(self.mainwin, text="提交", width=140, height=60, command=lambda: MyThread(self.commit)).place(x=60, y=118, width=61, height=30)
def commit(self):
pass
#继承 threading.Thread 类
class MyThread(threading.Thread):
def __init__(self, func, *args):
super().__init__()
self.func = func
self.args = args
self.setDaemon(True)
self.start() # 在这里开始
def run(self):
self.func(*self.args)