新手联系,使用python内置的tkinter模块来编写gui,本文是找的一个视频解析示例,可参考
# -*-coding:utf-8 -*-
# 借助第三方解析电影
# 环境python3,python2导包Tkinter
import re
import tkinter as tk
# 解析地址
from urllib import parse
# 消息盒子
import tkinter.messagebox as msgbox
# 控制浏览器
import webbrowser
class APP():
def __init__(self, width=500, height=300):
"""
:param width: 窗口宽
:param height: 窗口长
"""
self.w = width
self.h = height
self.title = "视频解析助手"
# tk对象
self.root = tk.Tk(className=self.title)
# 设置宽高
self.root.geometry("500x300")
# 用户输入的电影地址
self.url = tk.StringVar() # StringVar是tkinter专门用来接收字符串的方法,其他类型会报错
# 控制单选框默认选中的属性
self.v = tk.IntVar()
self.v.set(1) # 设置为选中
# 软件空间划分,区域划分
frame_1 = tk.Frame(self.root)
frame_2 = tk.Frame(self.root)
# 软件控件内容设置
group = tk.Label(frame_1, text="播放通道:", padx=10, pady=10)
# 单选框,value == self.v代表默认选中
tb = tk.Radiobutton(frame_1, text="唯一通道", variable=self.v, value=1, width=10, height=3)
# 空间2设置
label = tk.Label(frame_2, text="请输入视频播放地址:")
# 接收输入,heighlightcolor框的颜色,heighlightthickness输入框高度
entry = tk.Entry(frame_2, textvariable=self.url, highlightcolor="Fuchsia", highlightthickness=1, width=30)
# 播放按钮,command绑定函数事件
play = tk.Button(frame_2, text="播放", font=('楷体', 12), fg='Purple', width=2, height=1, command=self.video_play)
# 控件布局
"""
激活控件
"""
frame_1.pack()
frame_2.pack()
"""
位置确定
grid:宫格,九宫格类似
11 12 13
21 22 23
31 32 33
"""
group.grid(row=0, column=0)
tb.grid(row=0, column=1)
"""
空间2的控件位置,各个空间看自己的,与空间1无关
ipady:按钮给大小
"""
label.grid(row=0, column=0)
entry.grid(row=0, column=1)
play.grid(row=0, column=2, ipadx=10, ipady=10)
# 定义播放按钮的事件函数
def video_play(self):
# 第三方播放解析api
url = "http://www.wmxz.wang/video.php?url="
# 判断用户输入的电影地址是否合法
if re.match(r"https?:/{2}\w.+$", self.url.get()):
ip = self.url.get()
ip = parse.quote_plus(ip)
# 自动打开浏览器
webbrowser.open(url+ip)
else:
msgbox.showerror(title="错误", message="视频地址格式错误,https协议")
# 启动软件
def loop(self):
self.root.mainloop()
if __name__ == '__main__':
app = APP()
app.loop()
示例2:时间互转小工具
# -*-coding:utf-8 -*-
'''
@Time: 2021/7/27 18:08
@desc:
'''
import datetime
import time
import tkinter as tk
import tkinter.messagebox as msgbox
import traceback
from tkinter import ttk
class Conversion():
def __init__(self):
self.title = "时间转换工具"
self.tk = tk.Tk(className=self.title)
# 隐藏窗口
# self.tk.withdraw()
self.tk.attributes("-topmost",1 )
# 固定窗口大小
self.tk.resizable(width=False, height=False)
self.tk.geometry("800x300+500+300") # 大小,位置
# 区域划分,分三部分,1-时间戳转字符串,2-字符串转时间戳,3-日志
fram1 = tk.Frame(self.tk)
fram2 = tk.Frame(self.tk)
fram3 = tk.Frame(self.tk)
###################### fram1 #####################
# 时间戳标签
self.one_label = tk.Label(fram1, text="时间戳:")
# 时间戳文本框
self.enstr = tk.StringVar()
self.one_entry = tk.Entry(fram1, textvariable=self.enstr ,highlightcolor="Fuchsia", highlightthickness=1, width=20)
# 秒/毫秒转换切换
self.value = tk.StringVar()
self.com = ttk.Combobox(fram1, state='readonly', textvariable=self.value, width=5) # 只能选择
self.com["values"] = ("秒(s)", "毫秒(ms)")
self.com.current(0)
# 转换按钮
self.one_button = tk.Button(fram1, text="转换>>", font=('楷体', 12), fg='Purple', command=self.timestramp_to_str)
# 日期输出框
self.one_text = tk.Entry(fram1, width=30, highlightthickness=1, state=tk.NORMAL)
# 日期标签
self.two_label = tk.Label(fram1, text="北京时间:")
###################### fram2 #####################
# 时间标签
self.two_label = tk.Label(fram2, text="北京时间:")
# 时间字符串文本框
self.enstr2 = tk.StringVar()
self.two_entry = tk.Entry(fram2, highlightcolor="Fuchsia", textvariable=self.enstr2, highlightthickness=1, width=20)
# 转换按钮
self.two_button = tk.Button(fram2, text="转换>>", font=('楷体', 12), fg='Purple', command=self.str_to_timestramp)
# 时间戳输出框
self.two_text = tk.Entry(fram2, width=30, highlightthickness=2, state=tk.NORMAL)
# 秒/毫秒转换切换
self.value1 = tk.StringVar()
self.com_box = ttk.Combobox(fram2, state='readonly', textvariable=self.value1, width=5) # 只能选择
self.com_box["values"] = ("秒(s)", "毫秒(ms)")
self.com_box.current(0)
###################### fram3 #####################
self.three_label = tk.Label(fram3, text="现在时间戳:")
self.three_text = tk.Text(fram3, width=20, height=1, highlightthickness=1, padx=10)
self.three_text1 = tk.Text(fram3, width=20, height=1, highlightthickness=1, padx=10)
"""
激活组件
"""
fram3.pack(padx=10, pady=10)
fram1.pack(padx=10, pady=20)
fram2.pack(padx=10, pady=20)
"""
位置确定
"""
self.one_label.grid(row=0, column=0)
self.one_entry.grid(row=0, column=1)
self.com.grid(row=0, column=2)
self.one_button.grid(row=0, column=3)
self.two_label.grid(row=0, column=4)
self.one_text.grid(row=0, column=5)
self.two_label.grid(row=2, column=0)
self.two_entry.grid(row=2, column=1)
self.com_box.grid(row=2, column=2)
self.two_button.grid(row=2, column=3)
self.two_text.grid(row=2, column=4)
self.three_label.grid(row=0, column=0)
self.three_text1.grid(row=0, column=1)
# 重新定义方法启动,下拉选项默认值不展示
self.tk.after(1000, self.auto_time())
self.tk.mainloop()
def auto_time(self):
self.three_text1.delete('1.0', 'end')
self.three_text1.insert(tk.END, int(time.time()))
self.three_text.after(1000, self.auto_time) # again forever
def timestramp_to_str(self):
unit = self.value.get()
timestramp = self.enstr.get()
try:
timestramp = int(timestramp)
if unit == "毫秒(ms)":
timestramp = int(timestramp / 1000)
date_str = datetime.datetime.fromtimestamp(timestramp)
self.one_text.delete(0, tk.END)
self.one_text.insert(tk.END, date_str)
except:
print(traceback.format_exc())
msgbox.showerror(title="错误", message="请输入正确的正整数")
def str_to_timestramp(self):
date_str = self.enstr2.get()
unit = self.value1.get()
try:
timestramp = int(time.mktime(time.strptime(date_str, '%Y-%m-%d %H:%M:%S')))
if unit == "毫秒(ms)":
timestramp = int(timestramp * 1000)
self.two_text.delete(0, tk.END)
self.two_text.insert(tk.END, timestramp)
except:
print(traceback.format_exc())
msgbox.showerror(title="错误", message="请输入正确的日期格式,例:1970-01-01 08:00:00")
if __name__ == '__main__':
app = Conversion()
(1) 使用豆瓣源安装pyinstaller
pip install -i https://pypi.douban.com/simple/ pyinstaller
(2) 没配置环境变量找不到pyinstaller命令, 进入到安装pyinstaller,在python安装目录下Script文件夹下执行
pyinstaller -F -w -i D:\xxx\xxx\gui\1.ico D:\xxx\xxx\gui\video_gui.py
参数说明:
-i:应用程序的图标
-w: 不显示命令框
(3) 说明
打包完成后,exe文件在dist文件夹下,生成文件夹的地址控制台会输出如果加了图标,在dist下不会显示,拖出来就好了