-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
93 lines (79 loc) · 2.43 KB
/
test.py
File metadata and controls
93 lines (79 loc) · 2.43 KB
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
from tkinter import Tk, Entry
from __init__ import TkWebview as Webview
import ctypes
from threading import Thread
from time import sleep
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)/100
ctypes.windll.shcore.SetProcessDpiAwareness(1)
def get_url(event):
# 需要以协议(https, http, file等)开头
url=e.get()
web.navigate(url)
def count(req):
global count_num
count_num += req
return str(count_num)
def compute(returner, num1, num2):
def _(_1, _2):
sleep(1)
returner(_1 * _2)
Thread(
target=_,
args=(num1, num2),
).start()
a=Tk()
a.tk.call('tk', 'scaling', ScaleFactor)
a.geometry('800x400')
e=Entry(a,font='微软雅黑 18', relief='solid')
e.pack(fill='x', padx=10, pady=5)
e.bind('<Return>', get_url)
html="""
<div>
<button id=\"increment\">+</button>
<button id=\"decrement\">−</button>
<span>Counter: <span id=\"counterResult\">0</span></span>
</div>
<hr />
<div>
<button id=\"compute\">Compute</button>
<span>Result: <span id=\"computeResult\">(not started)</span></span>
</div>
<script type=\"module\">
const getElements = ids => Object.assign({}, ...ids.map(
id => ({ [id]: document.getElementById(id) })));
const ui = getElements([
\"increment\", \"decrement\", \"counterResult\", \"compute\",
\"computeResult\"
]);
ui.increment.addEventListener(\"click\", async () => {
ui.counterResult.textContent = await window.count(1);
});
ui.decrement.addEventListener(\"click\", async () => {
ui.counterResult.textContent = await window.count(-1);
});
ui.compute.addEventListener(\"click\", async () => {
ui.compute.disabled = true;
ui.computeResult.textContent = \"(pending)\";
ui.computeResult.textContent = await window.compute(6, 7);
ui.compute.disabled = false;
});
</script>"""
count_num = 0
test_mode = 1
if test_mode == 0:
web=Webview(master=a)
web.pack(fill='both', expand=True)
else:
web=Webview()
web.set_size(800, 400)
web.set_title("TkWebview Test")
web.set_html(html)
web.bindjs('count', count)
web.bindjs('compute', compute, is_async_return=True)
a.bind_all("<Button-1>", lambda e: e.widget.focus_force())
# 如果某个控件同样需要<Button-1>,可自行改写或者使用add参数添加
a.bind("<Alt-Left>", lambda e: web.go_back())
a.bind("<Alt-Right>", lambda e: web.go_forward())
a.bind("<F5>", lambda e: web.reload())
a.bind("<Escape>", lambda e: web.stop())
a.mainloop()