|
| 1 | +#!/usr/bin/python3 |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +"""Python便携版启动脚本 |
| 4 | +author: 小喾苦 |
| 5 | +date: 2022/8/27 19:51:06 |
| 6 | +QQ: 3434623263 |
| 7 | +bilibili: https://space.bilibili.com/513689605 |
| 8 | +personal website: https://xkk1.github.io/ |
| 9 | +""" |
| 10 | +import tkinter as tk |
| 11 | +import tkinter.filedialog |
| 12 | +import tkinter.scrolledtext |
| 13 | +import webbrowser |
| 14 | +import sys |
| 15 | +import os |
| 16 | +# import threading |
| 17 | +# import subprocess |
| 18 | + |
| 19 | + |
| 20 | +pyvi = sys.version_info |
| 21 | +python_version = f'{pyvi.major}.{pyvi.minor}.{pyvi.micro}' |
| 22 | + |
| 23 | + |
| 24 | +def run_idle(): |
| 25 | + os.system('start pythonw Lib\idlelib\idle.pyw') |
| 26 | + # def idle(): |
| 27 | + # os.system('pythonw Lib\idlelib\idle.pyw') |
| 28 | + # threading.Thread(target=idle).start() # , args=(old_verson, )) |
| 29 | + |
| 30 | +def run_python(): |
| 31 | + os.system('start python') |
| 32 | + # def python(): |
| 33 | + # subprocess.call( |
| 34 | + # 'python', |
| 35 | + # creationflags=subprocess.CREATE_NEW_CONSOLE) |
| 36 | + # threading.Thread(target=python).start() # , args=(old_verson, )) |
| 37 | + |
| 38 | +def run_manuals(): |
| 39 | + os.system(f'start Doc\python{pyvi.major}{pyvi.minor}{pyvi.micro}.chm') |
| 40 | + # def manuals(): |
| 41 | + # os.system(f'Doc\python{pyvi.major}{pyvi.minor}{pyvi.micro}.chm') |
| 42 | + # threading.Thread(target=manuals).start() # , args=(old_verson, )) |
| 43 | + |
| 44 | +def run_module_docs(): |
| 45 | + os.system('start python -m pydoc -b') |
| 46 | + # def module_docs(): |
| 47 | + # subprocess.call( |
| 48 | + # 'python -m pydoc -b', |
| 49 | + # creationflags=subprocess.CREATE_NEW_CONSOLE) |
| 50 | + # threading.Thread(target=module_docs).start() # , args=(old_verson, )) |
| 51 | + |
| 52 | +def run_py(): |
| 53 | + file_name = tkinter.filedialog.askopenfilename( |
| 54 | + title = '打开Python文件(Open Python File)', |
| 55 | + filetypes=[ |
| 56 | + ('Python File', '*.py'), |
| 57 | + ('Python File (no console)', '*.pyw'), |
| 58 | + ('All Files', '*')], |
| 59 | + #initialdir='C:\\Windows'), |
| 60 | + )#返回文件名--打开 |
| 61 | + # print(file_name) |
| 62 | + if file_name != '': |
| 63 | + file_path = '' |
| 64 | + for i in file_name.split('/')[:-1]: |
| 65 | + file_path += i + '/' |
| 66 | + os.system(f'cd /d {file_path} & start python "{file_name}"') |
| 67 | + """ |
| 68 | + def py(file_name): |
| 69 | + # os.system('pythonw "' + file_name + '"') |
| 70 | + # 打开新的命令提示符 |
| 71 | + file_path = '' |
| 72 | + for i in file_name.split('/')[:-1]: |
| 73 | + file_path += i + '/' |
| 74 | + temp = open('temporary_file.bat', 'w') |
| 75 | + temp.write('cd /d %s\n' % file_path) |
| 76 | + temp.write('start python "' + file_name + '"') |
| 77 | + # temp.write(' & PAUSE') |
| 78 | + temp.close() |
| 79 | + subprocess.call( |
| 80 | + 'temporary_file.bat', |
| 81 | + creationflags=subprocess.CREATE_NEW_CONSOLE) |
| 82 | + os.remove('temporary_file.bat') |
| 83 | + threading.Thread(target=py, args=(file_name, )).start() # , args=(old_verson, )) |
| 84 | + """ |
| 85 | + |
| 86 | +def run_pyw(): |
| 87 | + file_name = tkinter.filedialog.askopenfilename( |
| 88 | + title = '打开Python文件(Open Python File)', |
| 89 | + filetypes=[ |
| 90 | + ('Python File (no console)', '*.pyw'), |
| 91 | + ('Python File', '*.py'), |
| 92 | + ('All Files', '*')], |
| 93 | + ) |
| 94 | + if file_name != '': |
| 95 | + file_path = '' |
| 96 | + for i in file_name.split('/')[:-1]: |
| 97 | + file_path += i + '/' |
| 98 | + os.system(f'cd /d {file_path} & start pythonw "{file_name}"') |
| 99 | + |
| 100 | +def run_scripts(): |
| 101 | + os.system("cd Scripts & start cmd") |
| 102 | + """ |
| 103 | + def scripts(): |
| 104 | + temp = open('temporary_file.bat', 'w') |
| 105 | + temp.write('cd Scripts & cls & cmd') |
| 106 | + temp.close() |
| 107 | + subprocess.call( |
| 108 | + 'temporary_file.bat', |
| 109 | + # shell = True, |
| 110 | + creationflags=subprocess.CREATE_NEW_CONSOLE |
| 111 | + ) |
| 112 | + os.remove('temporary_file.bat') |
| 113 | + threading.Thread(target=scripts).start() |
| 114 | + """ |
| 115 | + |
| 116 | +def run_path(): |
| 117 | + os.system('start explorer %s' % os.path.abspath('.')) |
| 118 | + |
| 119 | +def show_information(information="", title="Python便携版信息", icon="Lib\idlelib\Icons\idle.ico"): |
| 120 | + """显示信息""" |
| 121 | + global information_window |
| 122 | + global information_scrolledtext |
| 123 | + |
| 124 | + def save_txt(information=information, title=title): |
| 125 | + filename = tkinter.filedialog.asksaveasfilename( |
| 126 | + title='请选择你要保存的地方', filetypes=[('TXT', '*.txt'), ('All Files', '*')], |
| 127 | + initialfile='%s' % title, |
| 128 | + defaultextension = 'txt', # 默认文件的扩展名 |
| 129 | + ) |
| 130 | + if filename == '': |
| 131 | + return False |
| 132 | + else: |
| 133 | + with open(filename, 'w') as f: |
| 134 | + f.write(information) |
| 135 | + return True |
| 136 | + try: |
| 137 | + information_window.deiconify() |
| 138 | + information_window.title(title) |
| 139 | + information_scrolledtext.delete(0.0, tk.END) |
| 140 | + information_scrolledtext.insert(tk.END, information) |
| 141 | + |
| 142 | + except: |
| 143 | + information_window = tk.Tk() |
| 144 | + information_window.title(title) |
| 145 | + try: # 尝试设置图标 |
| 146 | + information_window.iconbitmap(icon) |
| 147 | + except: |
| 148 | + pass |
| 149 | + information_scrolledtext = tkinter.scrolledtext.ScrolledText( |
| 150 | + information_window, |
| 151 | + width=70, |
| 152 | + height=30, |
| 153 | + undo=True |
| 154 | + ) |
| 155 | + information_scrolledtext.pack(expand=tk.YES, fill=tk.BOTH, padx=5, pady=5) |
| 156 | + |
| 157 | + information_scrolledtext.insert(tk.INSERT, information) |
| 158 | + |
| 159 | + '''创建一个弹出菜单''' |
| 160 | + menu = tk.Menu(information_window, |
| 161 | + tearoff=False, |
| 162 | + ) |
| 163 | + |
| 164 | + menu.add_command(label="剪切", command=lambda:information_scrolledtext.event_generate('<<Cut>>')) |
| 165 | + menu.add_command(label="复制", command=lambda:information_scrolledtext.event_generate('<<Copy>>')) |
| 166 | + menu.add_command(label="粘贴", command=lambda:information_scrolledtext.event_generate('<<Paste>>')) |
| 167 | + menu.add_command(label="删除", command=lambda:information_scrolledtext.event_generate('<<Clear>>')) |
| 168 | + menu.add_command(label="撤销", command=lambda:information_scrolledtext.event_generate('<<Undo>>')) |
| 169 | + menu.add_command(label="重做", command=lambda:information_scrolledtext.event_generate('<<Redo>>')) |
| 170 | + menu.add_separator() |
| 171 | + menu.add_command(label="个人网站", command=lambda:webbrowser.open("https://xkk1.github.io/")) |
| 172 | + menu.add_command(label="哔哩哔哩", command=lambda:webbrowser.open("https://space.bilibili.com/513689605")) |
| 173 | + |
| 174 | + def popup(event): |
| 175 | + menu.post(event.x_root, event.y_root) # post在指定的位置显示弹出菜单 |
| 176 | + |
| 177 | + information_scrolledtext.bind("<Button-3>", popup) # 绑定鼠标右键,执行popup函数 |
| 178 | + |
| 179 | + bottom_frame = tk.Frame(information_window) |
| 180 | + bottom_frame.pack() |
| 181 | + |
| 182 | + save_button = tk.Button( |
| 183 | + bottom_frame, |
| 184 | + text="保存为文本文档(*.txt)", |
| 185 | + command=lambda:save_txt(information=information_scrolledtext.get('1.0', tk.END).rstrip())) |
| 186 | + save_button.pack(side=tk.RIGHT, padx=5,pady=5) |
| 187 | + |
| 188 | + close_button = tk.Button( |
| 189 | + bottom_frame, |
| 190 | + text="关闭", |
| 191 | + command=information_window.destroy) |
| 192 | + close_button.pack(side=tk.RIGHT, padx=5,pady=5) |
| 193 | + |
| 194 | + def copy_to_clipboard(): |
| 195 | + """Copy current contents of text_entry to clipboard.""" |
| 196 | + information_window.clipboard_clear() # Optional. |
| 197 | + information_window.clipboard_append(information_scrolledtext.get('1.0', tk.END).rstrip()) |
| 198 | + |
| 199 | + copy_button = tk.Button( |
| 200 | + bottom_frame, |
| 201 | + text="复制内容到剪贴板", |
| 202 | + command=copy_to_clipboard, |
| 203 | + ) |
| 204 | + copy_button.pack(side=tk.LEFT, padx=5,pady=5) |
| 205 | + |
| 206 | + information_window.mainloop() |
| 207 | + |
| 208 | +def show_help(): |
| 209 | + """显示帮助信息""" |
| 210 | + show_information(information=f""" Python{python_version}便携版帮助 |
| 211 | +
|
| 212 | +运行Python脚本时将要运行的脚本托向open_python.bat即可 |
| 213 | +
|
| 214 | +run_python.bat支持的命令行参数: |
| 215 | + 所有python.exe支持的命令行参数 |
| 216 | + (注意:命令行参数不要超过9个) |
| 217 | +
|
| 218 | +卸载时只需把安装目录下的所有文件删除即可 |
| 219 | +安装目录:{os.path.abspath('.')} |
| 220 | +
|
| 221 | +
|
| 222 | +关于 |
| 223 | +Python {python_version} 便携版启动脚本 |
| 224 | +版本:V0.0.3 |
| 225 | +更新日志: |
| 226 | + 1、重写了打开程序逻辑 |
| 227 | + 2、优化了主窗口显示 |
| 228 | + 3、更详细的帮助 |
| 229 | + 4、帮助增加保存和复制功能 |
| 230 | +制作者:小喾苦 |
| 231 | +联系方式: |
| 232 | + 邮箱:3434623263@qq.com |
| 233 | + QQ:3434623263 |
| 234 | + 哔哩哔哩(www.bilibili.com): |
| 235 | + 用户名:小喾苦 |
| 236 | + UID:513689605 |
| 237 | + 空间:https://space.bilibili.com/513689605 |
| 238 | + 个人网站: |
| 239 | + Github Pages:https://xkk1.github.io/ |
| 240 | + Gitee Pages:https://xkk2.gitee.io/ |
| 241 | +""",title=f"Python {python_version} 便携版帮助信息") |
| 242 | + |
| 243 | + |
| 244 | +def main(): |
| 245 | + """程序入口,显示主窗口""" |
| 246 | + root = tk.Tk() |
| 247 | + root.title(f'Python {python_version}') # 标题 |
| 248 | + root.resizable(0,0) # 禁止调节窗口大小 |
| 249 | + try: # 尝试打开设置图标 |
| 250 | + root.iconbitmap('Lib\idlelib\Icons\idle.ico') |
| 251 | + except: |
| 252 | + pass |
| 253 | + |
| 254 | + frame = tk.Frame(root) |
| 255 | + frame.pack(padx=4, pady=4) |
| 256 | + |
| 257 | + button_idle = tk.Button(frame, text='启动\nIDLE', command=run_idle) |
| 258 | + button_idle.grid(row=0, column=0, padx=4, pady=4, sticky=tk.E+tk.W) |
| 259 | + |
| 260 | + button_python = tk.Button(frame, text=f'Python\n{python_version}', command=run_python) |
| 261 | + button_python.grid(row=0, column=1, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 262 | + |
| 263 | + button_manuals = tk.Button(frame, text=f'Python {python_version}\nManuals手册', command=run_manuals) |
| 264 | + button_manuals.grid(row=0, column=2, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 265 | + |
| 266 | + button_module_docs = tk.Button(frame, text=f'Python {python_version}\nModule Docs', command=run_module_docs) |
| 267 | + button_module_docs.grid(row=1, column=0, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 268 | + |
| 269 | + button_py = tk.Button(frame, text='运行\n*.py文件', command=run_py) |
| 270 | + button_py.grid(row=1, column=1, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 271 | + |
| 272 | + button_pyw = tk.Button(frame, text='运行\n*.pyw文件', command=run_pyw) |
| 273 | + button_pyw.grid(row=1, column=2, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 274 | + |
| 275 | + button_scripts = tk.Button(frame, text='Scripts\n可用pip等命令', command=run_scripts) |
| 276 | + button_scripts.grid(row=2, column=0, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 277 | + |
| 278 | + button_path = tk.Button(frame, text='打开\n工作目录', command=run_path) |
| 279 | + button_path.grid(row=2, column=1, padx=4, pady=4, sticky=tk.N+tk.S+tk.E+tk.W) |
| 280 | + |
| 281 | + button_help = tk.Button(frame, text='show help\n帮助信息', command=show_help) |
| 282 | + button_help.grid(row=2, column=2, padx=5, pady=5, sticky=tk.N+tk.S+tk.E+tk.W) |
| 283 | + |
| 284 | + root.mainloop() #循环消息,让窗口活起来 |
| 285 | + |
| 286 | + |
| 287 | +if __name__ == '__main__': |
| 288 | + if len(sys.argv) == 1: |
| 289 | + main() |
| 290 | + else: |
| 291 | + argv = '' |
| 292 | + for i in sys.argv[1:]: |
| 293 | + argv += ' "' + i +'"' |
| 294 | + file_name = sys.argv[1] |
| 295 | + file_path = '' |
| 296 | + for i in file_name.split('\\')[:-1]: |
| 297 | + file_path += i + '/' |
| 298 | + os.system(f'cd /d {file_path} & python {argv}') |
| 299 | + """ |
| 300 | + temp = open('temporary_file.bat', 'w') |
| 301 | + if file_path != '': |
| 302 | + temp.write('cd /d %s\n' % file_path) |
| 303 | + temp.write('"%~dp0python"' + argv + '') |
| 304 | + # temp.write(' & PAUSE') |
| 305 | + temp.close() |
| 306 | + os.system('temporary_file.bat') |
| 307 | + os.remove('temporary_file.bat') |
| 308 | + """ |
0 commit comments