FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PySimpleGUI/DemoPrograms/Demo_Chat_With_History.py at master · PySimpleGUI/PySimpleGUI · GitHub
PySimpleGUI
/
PySimpleGUI
Public
Notifications
You must be signed in to change notification settings
Fork
1.8k
Star
13.8k
Code
Issues
708
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PySimpleGUI
/
DemoPrograms
/
Demo_Chat_With_History.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
74 lines (57 loc) · 2.59 KB
Breadcrumbs
PySimpleGUI
/
DemoPrograms
/
Demo_Chat_With_History.py
Copy path
File metadata and controls
74 lines (57 loc) · 2.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
import
PySimpleGUI
as
sg
'''
A chatbot with history
Scroll up and down through prior commands using the arrow keys
Special keyboard keys:
Up arrow - scroll up in commands
Down arrow - scroll down in commands
Escape - clear current command
Control C - exit form
Copyright 2018-2026 PySimpleGUI. All rights reserved.
'''
def
ChatBotWithHistory
():
# ------- Make a new Window ------- #
# give our form a spiffy set of colors
sg
.
theme
(
'GreenTan'
)
layout
=
[[
sg
.
Text
(
'Your output will go here'
,
size
=
(
40
,
1
))],
[
sg
.
Output
(
size
=
(
127
,
30
),
font
=
(
'Helvetica 10'
))],
[
sg
.
Text
(
'Command History'
),
sg
.
Text
(
''
,
size
=
(
20
,
3
),
key
=
'history'
)],
[
sg
.
ML
(
size
=
(
85
,
5
),
enter_submits
=
True
,
key
=
'query'
,
do_not_clear
=
False
),
sg
.
Button
(
'SEND'
,
button_color
=
(
sg
.
YELLOWS
[
0
],
sg
.
BLUES
[
0
]),
bind_return_key
=
True
),
sg
.
Button
(
'EXIT'
,
button_color
=
(
sg
.
YELLOWS
[
0
],
sg
.
GREENS
[
0
]))]]
window
=
sg
.
Window
(
'Chat window with history'
,
layout
,
default_element_size
=
(
30
,
2
),
font
=
(
'Helvetica'
,
' 13'
),
default_button_element_size
=
(
8
,
2
),
return_keyboard_events
=
True
)
# ---===--- Loop taking in user input and using it --- #
command_history
=
[]
history_offset
=
0
while
True
:
event
,
value
=
window
.
read
()
if
event
==
'SEND'
:
query
=
value
[
'query'
].
rstrip
()
# EXECUTE YOUR COMMAND HERE
print
(
'The command you entered was {}'
.
format
(
query
))
command_history
.
append
(
query
)
history_offset
=
len
(
command_history
)
-
1
# manually clear input because keyboard events blocks clear
window
[
'query'
].
update
(
''
)
window
[
'history'
].
update
(
'
\n
'
.
join
(
command_history
[
-
3
:]))
elif
event
in
(
sg
.
WIN_CLOSED
,
'EXIT'
):
# quit if exit event or X
break
elif
'Up'
in
event
and
len
(
command_history
):
command
=
command_history
[
history_offset
]
# decrement is not zero
history_offset
-=
1
*
(
history_offset
>
0
)
window
[
'query'
].
update
(
command
)
elif
'Down'
in
event
and
len
(
command_history
):
# increment up to end of list
history_offset
+=
1
*
(
history_offset
<
len
(
command_history
)
-
1
)
command
=
command_history
[
history_offset
]
window
[
'query'
].
update
(
command
)
elif
'Escape'
in
event
:
window
[
'query'
].
update
(
''
)
ChatBotWithHistory
()
Back
|
FazBrowse Home
|
New Git URL