大家好,又见面了,我是你们的朋友全栈君。
在日常开发中,总需要一些普通的小工具。小工具嘛,要得急,写得也急,总有很多不完善的问题,频繁修改成了一个较大的问题。比如之前用c#
写了一个将excel
表自动转成csv
文本的工具,后面需要一个转为Tab
分割的文本,本来也就是改一点小东西,重新编译一下就可以,但是工程不小心丢了,确实比较尴尬了。
python
脚本,lua
也值得考虑,不过桌面上的功能不及python
强大python
环境,所以使用了C++
内嵌一个python
解释器python
的一些第三方库visual studio
创建一个C++
的工程。这工程可以随意丢弃,因为生成玩一次就基本可以不用再打开了C++
要使用到Python.h
头文件,以及python.lib
静态库Windows x86-64 embeddable zip file
,https://www.python.org/ftp/python/3.7.3/python-3.7.3-embed-amd64.zipmain.cpp
文件,点击Release
生成。#include <iostream>
#include "Python.h"
int main(int argc,char *args[])
{
const std::wstring path = L"python37.zip;site-packages;";
Py_SetPath(path.c_str());
Py_Initialize();
const char* pyFile = "main.py";
//取外部python文件
if (argc > 1)
{
pyFile = args[1];
}
PyObject *obj = Py_BuildValue("s", pyFile);
FILE *fp = _Py_fopen_obj(obj, "r");
if (fp == NULL)
return 1;
PyRun_SimpleFile(fp, pyFile);
Py_Finalize();
return 0;
}
python37.zip
,拷贝到一个文件夹中,同时在文件夹下创建一个site-packages文件夹用来存第三方的库,一个开发工具就已经完成了。python
环境中,安装好第三方库,然后将Lib\site-packages
里面想使用的库拷贝到site-packages
文件夹下,比如我这里就使用了wxpython
的库python
脚本了,创建一个UploadEditor.py
脚本,里面就使用了wxpython
实现了一个简单的界面import wx
from shutil import copyfile
import os
import uuid
#上传编辑器
class UploadEditor(wx.Frame):
def __init__(self, *args, **kw):
super(UploadEditor,self).__init__(*args, **kw)
pnl=wx.Panel(self)
self.textCtrl = wx.TextCtrl(pnl,wx.ID_ANY,value="",pos=(3,5),size=(375,160),style=wx.TE_READONLY|wx.TE_NOHIDESEL|wx.TE_MULTILINE)
# textCtrl.IsMultiLine=true
#菜单栏
self.MakeMenuBar()
#状态栏
self.CreateStatusBar()
self.SetStatusText("\\\\win-yjtlopmtrcn -- Administrator:Asd997asd")
#创建菜单栏
def MakeMenuBar(self):
fileMenu=wx.Menu()
openItem = fileMenu.Append(-1,"Open")
exitItem = fileMenu.Append(wx.ID_EXIT)
#创建菜单栏
menuBar=wx.MenuBar()
menuBar.Append(fileMenu,"&File")
#设置菜单栏
self.SetMenuBar(menuBar)
#绑定事件
self.Bind(wx.EVT_MENU,self.OnExit,exitItem)
self.Bind(wx.EVT_MENU,self.OnOpen,openItem)
#退出事件
def OnExit(self,event):
self.Close(True)
#打开事件
def OnOpen(self,event):
# otherwise ask the user what new file to open
with wx.FileDialog(self, "Open image file", wildcard="image files (*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST| wx.FD_MULTIPLE) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return # the user changed their mind
# Proceed loading the file chosen by the user
paths = fileDialog.GetPaths()
#更新名称的文件集合
mdPaths=[]
self.textCtrl.flush()
for pathName in paths:
try:
newFile = self.CopyFiles(pathName)
mdPaths.append(newFile)
mdPath="!["+os.path.basename(pathName)+"]("+newFile+")";
self.textCtrl.write(mdPath+"\n\n")
except IOError:
wx.LogError("Cannot open file '%s'." % newfile)
#复制文件
def CopyFiles(self,filePath):
ext=os.path.splitext(filePath)[1]
filePath=os.path.abspath(filePath)
fileName=str(uuid.uuid1())+ext
#目录路径
targetPath=os.path.join("\\\\win-yjtlopmtrcn\\load\\images",fileName)
#copyfile(filePath,targetPath)
#复制文件 windows xcopy命令
os.system("copy \""+filePath+"\" \""+targetPath+"\" /Y")
#mdPath=os.path.join("http://win-yjtlopmtrcn/load/images",fileName)
mdPath="http://win-yjtlopmtrcn/load/images/"+fileName
return mdPath
main.py
文件import wx
from UploadEditor import UploadEditor
app= wx.App()
frm= UploadEditor(None,title="图片上传工具")
frm.Show()
app.MainLoop()
exe
或者使用命令EPython.exe main.py
就可以正常运行wxpython
的库太大了,80M,不使用wxpython
最多几M。不过基本逻辑没有问题,借助这种方式可以快速修改或者开发小工具了。python
脚本,原来上传图片的地址,是本地局域网的地址,可以改成自己本机的地址。csdn
的资源管理,不知道是怎么回事,上传的资源自动判断分数,本来没想收积分的。发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143854.html原文链接:https://javaforall.cn