Skip to content

Commit d87886e

Browse files
committed
many
1 parent 2394601 commit d87886e

File tree

8 files changed

+216
-0
lines changed

8 files changed

+216
-0
lines changed

text-game/example-0.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
def first_room():
2+
print("Your in a room how to get out...")
3+
4+
looking_forward = False
5+
looking_backward = False
6+
7+
while True:
8+
question = input().lower()
9+
if question == "look forward":
10+
if not looking_forward:
11+
print("You are looking forward")
12+
looking_forward = True
13+
looking_backward = False
14+
else:
15+
print("You are already looking forward, so what next")
16+
elif question == 'look backward':
17+
if not looking_backward:
18+
print("You are looking backward")
19+
looking_forward = False
20+
looking_backward = True
21+
else:
22+
print("You are already looking backward, so what next")
23+
elif question == 'go there':
24+
if looking_forward:
25+
return second_room()
26+
if looking_backward:
27+
return third_room()
28+
else:
29+
print('Go where ???')
30+
31+
32+
def second_room():
33+
print("This is the second room")
34+
35+
def third_room():
36+
print("This is the third room")
37+
38+
# --------------------------------
39+
40+
first_room()

text-game/example-1.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def first_room():
2+
print("Your in a room how to get out...")
3+
4+
looking_forward = False
5+
looking_backward = False
6+
7+
while True:
8+
question = input().lower()
9+
if question == "look forward":
10+
if not looking_forward:
11+
print("You are looking forward")
12+
looking_forward = True
13+
looking_backward = False
14+
else:
15+
print("You are already looking forward, so what next")
16+
elif question == 'look backward':
17+
if not looking_backward:
18+
print("You are looking backward")
19+
looking_forward = False
20+
looking_backward = True
21+
else:
22+
print("You are already looking backward, so what next")
23+
elif question == 'go there':
24+
if looking_forward:
25+
return second_room
26+
if looking_backward:
27+
return third_room
28+
else:
29+
print('Go where ???')
30+
31+
32+
def second_room():
33+
print("This is the second room")
34+
35+
36+
def third_room():
37+
print("This is the third room")
38+
39+
# --------------------------------
40+
41+
next_room = first_room
42+
43+
while next_room is not None:
44+
next_room = next_room()
45+
46+
print("Good Bye")

text-game/example-2.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
rooms = {
2+
'room1': {
3+
'description': "Your in a room how to get out...",
4+
},
5+
'room2': {
6+
'description': "This is the second room",
7+
},
8+
'room3': {
9+
'description': "This is the third room",
10+
},
11+
}
12+
13+
def show_room(name):
14+
print(rooms[name]['description'])
15+
16+
star
17+
looking_forward = False
18+
looking_backward = False
19+
20+
while True:
21+
answer = input().lower()
22+
if answer == 'exit':
23+
print("Good Bye")
24+
return
25+
if answer == "look forward":
26+
if not looking_forward:
27+
print("You are looking forward")
28+
looking_forward = True
29+
looking_backward = False
30+
else:
31+
print("You are already looking forward, so what next")
32+
elif answer == 'look backward':
33+
if not looking_backward:
34+
print("You are looking backward")
35+
looking_forward = False
36+
looking_backward = True
37+
else:
38+
print("You are already looking backward, so what next")
39+
elif answer == 'go there':
40+
if looking_forward:
41+
return 'room2'
42+
if looking_backward:
43+
return 'room3'
44+
else:
45+
print('Go where ???')
46+
47+
# --------------------------------
48+
49+
name = 'room1'
50+
51+
while name is not None:
52+
name = show_room(name)
53+

tkinter/font/display-names.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
3+
import tkinter as tk
4+
5+
from tkinter.font import families
6+
root = tk.Tk()
7+
8+
families = tk.font.families()
9+
print('\nnumber:', len(families))
10+
print('\n'.join(sorted(families)))
11+
12+
names = tk.font.names()
13+
print('\nnumber:', len(names))
14+
print('\n'.join(sorted(names)))
15+

tkinter/listbox/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
## event-listboxselect-get-curselection.py
3+
4+
It shows how to use event `<<ListboxSelect>>` to execute function after selectiong row on list.
5+
6+
It also shows how to use `event` to get access to `Listbox` and get setlected element.
7+
8+
---
9+
10+
## listbox-populate-entry.py
11+
12+
![#1](images/listbox-populate-entry.png?raw=true)
13+
14+
15+
After click on list `<<ListboxSelect>>` executes function which gets data from list (as single string)
16+
splits it to three elements using `slicing` and `strip()`, and puts elements in three `Entries`
17+
18+
Program use
19+
20+
- `"{:10s}|{:15s}|{:10s}"` to format columns in text,
21+
- monospaced font `font=('monospace', 10)` to correctly display columns on list.
22+
23+
---
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import tkinter as tk
2+
3+
# --- function ---
4+
5+
def on_selection(event):
6+
line = event.widget.get(event.widget.curselection())
7+
8+
e1.delete(0, 'end')
9+
e2.delete(0, 'end')
10+
e3.delete(0, 'end')
11+
12+
e1.insert('end', line[:10].strip())
13+
e2.insert('end', line[11:26].strip())
14+
e3.insert('end', line[27:].strip())
15+
16+
# --- main ---
17+
18+
root = tk.Tk()
19+
root.geometry('400x300')
20+
21+
listbox = tk.Listbox(root, font=('monospace', 10), selectbackground='red')
22+
listbox.pack(expand='yes', fill='both')
23+
24+
listbox.insert('end', '{:10s}|{:15s}|{:10s}'.format('123', 'Bob', 'Active'))
25+
listbox.insert('end', '{:10s}|{:15s}|{:10s}'.format('212', 'Jim', 'Off'))
26+
listbox.insert('end', '{:10s}|{:15s}|{:10s}'.format('417', 'Johny', 'Off'))
27+
28+
listbox.bind('<<ListboxSelect>>', on_selection)
29+
30+
e1 = tk.Entry(root)
31+
e1.pack()
32+
33+
e2 = tk.Entry(root)
34+
e2.pack()
35+
36+
e3 = tk.Entry(root)
37+
e3.pack()
38+
39+
root.mainloop()

0 commit comments

Comments
 (0)