需要先安装依赖
pip install wxPython
界面代码:
# coding=utf-8
import configparser
import os
import sys
import re
import time
import threading
import wx
class MyApp(wx.Frame):
def __init__(self, window, properties):
wx.Frame.__init__(
self, window, id=wx.ID_ANY,
title=properties.get("title"),
pos=wx.DefaultPosition,
size=wx.Size(properties.get("size")),
style=wx.DEFAULT_FRAME_STYLE ^ wx.MAXIMIZE_BOX
)
self.ini = os.path.join(os.environ["appdata"], 'my_app.ini')
self.configuration = None
self.support_product = ["A", "B", "C", "D", "E", "F", "G", "H","I", "J"]
self.support_version_map = {
"A": ["AA", "AB"],
"B": ["BB", "BCD", "BFD"],
"C": ["CL"],
"D": ["DD"],
"E": ["EE", "EA"],
"F": ["FF", "FE", "FG", "FFF", "F1"],
"G": ["GG"],
"H": ["HZ", "HP"],
"I": ["I"],
"J": ["JJ", "JP"],
}
self.dst_version = ["ABC", "10JK", "JQK", "10JQKA"]
self.help_info = "使用说明:\n1、read me\n"
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetExtraStyle(wx.FRAME_EX_METAL)
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENU))
table = wx.GridBagSizer(0, 0)
table.SetFlexibleDirection(wx.BOTH)
table.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)
label_file = wx.StaticText(self, wx.ID_ANY, '文件路径', wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_RIGHT)
label_file.Wrap(-1)
label_file.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
table.Add(label_file, wx.GBPosition(1, 1), wx.GBSpan(1, 1), wx.ALL, 5)
self.file = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, '选择文件', '*.*', wx.DefaultPosition,
wx.Size(550, -1), wx.FLP_DEFAULT_STYLE)
self.file.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
table.Add(self.file, wx.GBPosition(1, 2), wx.GBSpan(1, 5), wx.ALL, 5)
self.run_button = wx.Button(self, wx.ID_ANY, '运行', wx.DefaultPosition, wx.Size(120, -1), wx.NO_BORDER)
self.run_button.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
self.run_button.SetForegroundColour("#ffffff")
self.run_button.SetBackgroundColour("#2fa4e7")
table.Add(self.run_button, wx.GBPosition(1, 7), wx.GBSpan(1, 1), wx.ALL, 5)
label_src_product = wx.StaticText(self, wx.ID_ANY, '选择A', wx.DefaultPosition, wx.DefaultSize,
wx.ALIGN_RIGHT)
label_src_product.Wrap(-1)
label_src_product.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
table.Add(label_src_product, wx.GBPosition(2, 1), wx.GBSpan(1, 1), wx.ALL, 5)
choice_src_product = self.get_src_product_list()
self.choice_src_product = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(135, -1), choice_src_product,
0)
self.choice_src_product.SetSelection(0)
table.Add(self.choice_src_product, wx.GBPosition(2, 2), wx.GBSpan(1, 1), wx.ALL, 5)
label_src_version = wx.StaticText(self, wx.ID_ANY, 'A联动选择', wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_RIGHT)
label_src_version.Wrap(-1)
label_src_version.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
table.Add(label_src_version, wx.GBPosition(2, 3), wx.GBSpan(1, 1), wx.ALL, 5)
choice_src_version = self.get_src_version_list()
self.choice_src_version = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(135, -1), choice_src_version,
0)
self.choice_src_version.SetSelection(0)
table.Add(self.choice_src_version, wx.GBPosition(2, 4), wx.GBSpan(1, 1), wx.ALL, 5)
label_dst_version = wx.StaticText(self, wx.ID_ANY, '选择B', wx.DefaultPosition, wx.Size(-1, 30), wx.ALIGN_CENTRE)
label_dst_version.Wrap(-1)
label_dst_version.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
table.Add(label_dst_version, wx.GBPosition(2, 5), wx.GBSpan(1, 1), wx.ALL, 5)
self.choice_dst_version = wx.Choice(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(135, -1), self.dst_version, 0)
self.choice_dst_version.SetSelection(0)
table.Add(self.choice_dst_version, wx.GBPosition(2, 6), wx.GBSpan(1, 1), wx.ALL, 5)
clean_log = wx.Button(self, wx.ID_ANY, '清空日志', wx.DefaultPosition, wx.Size(120, -1), wx.NO_BORDER)
clean_log.SetFont(wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '微软雅黑'))
clean_log.SetForegroundColour("#ffffff")
clean_log.SetBackgroundColour("#2fa4e7")
table.Add(clean_log, wx.GBPosition(2, 7), wx.GBSpan(1, 1), wx.ALL, 5)
self.log = wx.TextCtrl(self, wx.ID_ANY, self.help_info, wx.DefaultPosition, wx.Size(750, 400), wx.TE_MULTILINE)
self.log.SetFont(wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, '宋体'))
table.Add(self.log, wx.GBPosition(3, 1), wx.GBSpan(1, 9), wx.ALL, 5)
self.gauge_bar = wx.Gauge(self, wx.ID_ANY, 0, wx.DefaultPosition, wx.Size(750, -1), wx.GA_HORIZONTAL)
table.Add(self.gauge_bar, wx.GBPosition(4, 1), wx.GBSpan(1, 9), wx.ALL, 5)
self.SetSizer(table)
self.Layout()
self.Centre(wx.BOTH)
self.choice_src_product.Bind(wx.EVT_CHOICE, self.change_select_source_product)
self.choice_src_version.Bind(wx.EVT_CHOICE, self.change_help_info)
self.run_button.Bind(wx.EVT_BUTTON, self.main)
clean_log.Bind(wx.EVT_BUTTON, self.clean_log_textarea)
self.load_config()
def load_config(self):
if not os.path.exists(self.ini):
return
try:
config = configparser.ConfigParser()
config.read(self.ini, encoding="utf-8")
self.file.SetPath(config.get("file", "path"))
self.choice_src_product.SetSelection(config.getint("select", "src_product"))
self.change_select_source_product("")
self.choice_src_version.SetSelection(config.getint("select", "src_version"))
self.choice_dst_version.SetSelection(config.getint("select", "dst_version"))
self.change_help_info("")
except Exception as e:
print("[Error] load_config {}".format(e))
def save_config(self):
config = configparser.ConfigParser()
config.add_section("file")
config.set("file", "path", self.file.GetPath())
config.add_section("select")
config.set("select", "src_product", str(self.choice_src_product.GetCurrentSelection()))
config.set("select", "src_version", str(self.choice_src_version.GetCurrentSelection()))
config.set("select", "dst_version", str(self.choice_dst_version.GetCurrentSelection()))
config.write(open(self.ini, "w", encoding="utf-8"))
def clean_log_textarea(self, _):
self.log.Clear()
self.log.AppendText(self.help_info)
self.gauge_bar.SetValue(0)
def write_program_2_log_textarea(self, info):
self.log.AppendText("="*40+info+"="*40+"\n")
def write_2_log_textarea(self, info):
timestamp = time.strftime('%H:%M:%S', time.localtime(time.time()))
self.log.AppendText("[{}]{}\n".format(timestamp, info))
def change_select_source_product(self, _):
product = self.choice_src_product.GetString(self.choice_src_product.GetSelection())
version = self.support_version_map.get(product, [])
self.choice_src_version.SetItems(version)
if version:
self.choice_dst_version.SetSelection(0)
def get_src_product_list(self):
return self.support_product
def get_src_version_list(self):
product = self.choice_src_product.GetString(self.choice_src_product.GetSelection())
return self.support_version_map.get(product, [])
def change_help_info(self, _):
version = self.choice_src_version.GetString(self.choice_src_version.GetSelection())
help_info = "{}的使用说明".format(version)
self.log.Clear()
self.log.AppendText(help_info)
def main(self, _):
self.run_button.Enable(False)
self.save_config()
t = threading.Thread(target=self.back_job, args=())
t.start()
def back_job(self):
self.log.Clear()
self.gauge_bar.SetValue(0)
self.gauge_bar.SetRange(1)
self.write_program_2_log_textarea("操作开始")
self.configuration = self.file.GetPath().strip()
self.write_2_log_textarea("file: {}".format(self.configuration))
product = self.choice_src_product.GetString(self.choice_src_product.GetSelection())
version = self.choice_src_version.GetString(self.choice_src_version.GetSelection())
self.write_2_log_textarea("source product: {} source version: {}".format(product, version))
self.run_button.Enable(True)
self.gauge_bar.SetValue(1)
self.write_program_2_log_textarea("操作结束")
def app_gui_show(version):
app_name = "小工具"
app = wx.App()
filename = sys.argv[0]
ma = re.search(r"(v[\d.]+)\.exe", filename)
if ma:
version = ma.group(1)
properties = {"title": "{} {}".format(app_name, version), "size": (800, 600)}
window = MyApp(None, properties)
# tmp = open("tmp.ico", "wb+")
# tmp.write(base64.b64decode(logo.ico))
# tmp.close()
# icon = wx.Icon(name="tmp.ico", type=wx.BITMAP_TYPE_ICO)
# window.SetIcon(icon)
window.Show(True)
# os.remove("tmp.ico")
app.MainLoop()
app_gui_show("v1.0")
界面效果: