-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1t.py
181 lines (156 loc) · 6.49 KB
/
1t.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import wx
import wx.html2
class WebViewPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
# Create the WebView control
self.web_view = wx.html2.WebView.New(self)
# Attach custom scheme handler
self.attach_custom_scheme_handler()
# Bind navigation and error events
self.web_view.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.on_navigating)
self.web_view.Bind(wx.html2.EVT_WEBVIEW_ERROR, self.on_webview_error)
# Set initial HTML content
self.set_initial_content()
# Create sizer to organize the WebView
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.web_view, 1, wx.EXPAND)
self.SetSizer(sizer)
def attach_custom_scheme_handler(self):
handler = CustomSchemeHandler(self)
self.web_view.RegisterHandler(handler)
def set_initial_content(self):
initial_html = """
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
#header-container {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
#header-container h1 {
margin: 0;
}
#url-input {
width: 300px;
padding: 5px;
}
#input-container {
margin: 20px 0;
display: flex;
align-items: flex-start;
}
#user-input {
width: 300px;
height: 100px;
padding: 5px;
resize: vertical;
margin-right: 10px;
}
#start-button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
}
#start-button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="header-container">
<h1>Welcome to the WebView Panel!</h1>
<input type="text" id="url-input" placeholder="Enter URL here">
</div>
<div id="input-container">
<textarea id="user-input" placeholder="Enter your text here"></textarea>
<button id="start-button" onclick="startButtonClicked()">Start</button>
</div>
<div id="output"></div>
<script>
function startButtonClicked() {
var userInput = document.getElementById('user-input').value;
var url = document.getElementById('url-input').value;
document.getElementById('output').innerHTML = 'You entered: ' + userInput.replace(/\\n/g, '<br>') + '<br>URL: ' + url;
window.location.href = 'app:start?input=' + encodeURIComponent(userInput) + '&url=' + encodeURIComponent(url);
}
</script>
</body>
</html>
"""
self.web_view.SetPage(initial_html, "")
def on_start_button(self, user_input, url):
print(f"Start button clicked with input:\n{user_input}")
print(f"URL: {url}")
wx.MessageBox(f"You entered:\n{user_input}\n\nURL: {url}", "Input Received", wx.OK | wx.ICON_INFORMATION)
def on_navigating(self, event):
url = event.GetURL()
print(f"Navigating to: {url}")
if url.startswith("app:"):
event.Veto() # Prevent actual navigation for our custom scheme
def on_webview_error(self, event):
print(f"WebView error: {event.GetString()}")
def update_html_content(self, new_content):
self.web_view.SetPage(new_content, "")
class CustomSchemeHandler(wx.html2.WebViewHandler):
def __init__(self, web_view_panel):
wx.html2.WebViewHandler.__init__(self, "app")
self.web_view_panel = web_view_panel
def OnRequest(self, webview, request):
print(f"OnRequest called with URL: {request.GetURL()}")
if request.GetResourceType() == wx.html2.WEBVIEW_RESOURCE_TYPE_MAIN_FRAME:
url = request.GetURL()
if url.startswith("app:start?"):
# Extract the user input and URL from the request
parsed_url = wx.URL(url)
user_input = parsed_url.GetQueryParameter("input")
input_url = parsed_url.GetQueryParameter("url")
wx.CallAfter(self.web_view_panel.on_start_button, user_input, input_url)
return None
class MyApp(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='WebView Panel with Chat and Notebook')
panel = wx.Panel(self)
# Create a notebook control
self.notebook = wx.Notebook(panel)
# Create an instance of WebViewPanel
self.web_view_panel = WebViewPanel(self.notebook)
# Add the WebViewPanel to the notebook with the label "Titles"
self.notebook.AddPage(self.web_view_panel, "Titles")
# Create a multiline TextCtrl (read-only)
self.text_ctrl = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_READONLY)
# Create a "Chat" button
chat_button = wx.Button(panel, label='Chat')
chat_button.Bind(wx.EVT_BUTTON, self.on_chat)
# Create sizer to manage layout
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.notebook, 1, wx.EXPAND | wx.ALL, 10)
main_sizer.Add(self.text_ctrl, 1, wx.EXPAND | wx.ALL, 10)
main_sizer.Add(chat_button, 0, wx.ALIGN_CENTER | wx.BOTTOM, 10)
panel.SetSizer(main_sizer)
self.SetSize((800, 600))
self.Show()
def on_chat(self, event):
self.text_ctrl.AppendText("Chat button clicked!\n")
# Update the HTML content in the WebView
new_html_content = """
<html>
<body>
<h1>Chat Button Clicked!</h1>
<p>This content was updated when you clicked the Chat button.</p>
<button onclick="alert('Hello from updated content!')">Click me</button>
</body>
</html>
"""
self.web_view_panel.update_html_content(new_html_content)
if __name__ == '__main__':
app = wx.App()
frame = MyApp()
app.MainLoop()