-
Notifications
You must be signed in to change notification settings - Fork 19
/
banner_tools.py
106 lines (89 loc) · 3.43 KB
/
banner_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Author: Acer Zhang
# Datetime: 2021/9/16
# Copyright belongs to the author.
# Please indicate the source for reprinting.
import os
import webbrowser
import tkinter
from tkinter import ttk
from qgui.manager import ICON_PATH, ConcurrencyModeFlag
from qgui.base_tools import ArgInfo, BaseTool
RUN_ICON = os.path.join(ICON_PATH, "play_w.png")
GITHUB_ICON = os.path.join(ICON_PATH, "github.png")
AI_STUDIO_ICON = os.path.join(ICON_PATH, "up_cloud.png")
class BaseBarTool(BaseTool):
"""
基础Banner工具集
需注意的是,如需增加异步等操作,请为函数添加_callback
"""
def __init__(self,
bind_func,
name="未命名组件",
icon=None,
style=None,
async_run: bool = True,
concurrency_mode=ConcurrencyModeFlag.SAFE_CONCURRENCY_MODE_FLAG):
super().__init__(bind_func=bind_func,
name=name,
style=style,
async_run=async_run,
concurrency_mode=concurrency_mode)
if icon and not os.path.exists(icon):
raise f"请确认{os.path.abspath(icon)}是否存在"
if not icon:
icon = RUN_ICON
self.icon = icon
def build(self, *args, **kwargs):
super().build(*args, **kwargs)
self.img = tkinter.PhotoImage(file=self.icon)
btn = ttk.Button(self.master,
text=self.name,
image=self.img,
compound="left",
command=self._callback(self.bind_func) if self.async_run else self.bind_func,
style=self.style + "TButton")
btn.pack(side="left", ipadx=5, ipady=5, padx=0, pady=1)
class RunTool(BaseBarTool):
def __init__(self,
bind_func,
name="开始执行",
icon=None,
style="success",
async_run: bool = True,
concurrency_mode=ConcurrencyModeFlag.SAFE_CONCURRENCY_MODE_FLAG):
if not icon:
icon = RUN_ICON
super(RunTool, self).__init__(bind_func,
name=name,
icon=icon,
style=style,
async_run=async_run,
concurrency_mode=concurrency_mode)
class GitHub(BaseBarTool):
def __init__(self,
url,
name="在GitHub上查看该项目",
style="primary"):
icon = GITHUB_ICON
bind_func = self.github_callback
super().__init__(bind_func,
name=name,
icon=icon,
style=style)
self.github_url = url
def github_callback(self, args):
webbrowser.open_new(self.github_url)
class AIStudio(BaseBarTool):
def __init__(self,
url,
name="在AI Studio云端环境上使用(推荐)",
style="primary"):
icon = AI_STUDIO_ICON
bind_func = self.ai_studio_callback
super().__init__(bind_func,
name=name,
icon=icon,
style=style)
self.ai_studio_url = url
def ai_studio_callback(self, args):
webbrowser.open_new(self.ai_studio_url)