Skip to content

Commit 596a547

Browse files
committed
dataframe
1 parent 1e01e74 commit 596a547

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
Minimal example of scenes (using functions)
3+
4+
Source: https://stackoverflow.com/a/47460947/1832058
5+
Author: https://stackoverflow.com/users/6220679/skrx
6+
7+
---
8+
9+
Other example: https://gist.github.com/iminurnamez/8d51f5b40032f106a847
10+
11+
---
12+
13+
Wikipedia: [Finite-State Machine](https://en.wikipedia.org/wiki/Finite-state_machine)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# source: https://stackoverflow.com/a/47460947/1832058
5+
# author: https://stackoverflow.com/users/6220679/skrx
6+
#
7+
# minimal example of scenes (using functions)
8+
#
9+
10+
import pygame as pg
11+
12+
pg.init()
13+
screen = pg.display.set_mode((600, 400))
14+
clock = pg.time.Clock()
15+
BLUE = pg.Color('dodgerblue3')
16+
ORANGE = pg.Color('sienna3')
17+
18+
19+
def front_page():
20+
while True:
21+
for event in pg.event.get():
22+
if event.type == pg.QUIT:
23+
return None
24+
# Press a key to return the next scene.
25+
elif event.type == pg.KEYDOWN:
26+
return menu
27+
28+
screen.fill(BLUE)
29+
pg.display.flip()
30+
clock.tick(60)
31+
32+
33+
def menu():
34+
while True:
35+
for event in pg.event.get():
36+
if event.type == pg.QUIT:
37+
return None
38+
# Press a key to return the next scene.
39+
elif event.type == pg.KEYDOWN:
40+
return front_page
41+
42+
screen.fill(ORANGE)
43+
pg.display.flip()
44+
clock.tick(60)
45+
46+
47+
def main():
48+
scene = front_page # Set the current scene.
49+
while scene is not None:
50+
for event in pg.event.get():
51+
if event.type == pg.QUIT:
52+
scene = None
53+
54+
# Execute the current scene function. When it's done
55+
# it returns either the next scene or None which we
56+
# assign to the scene variable.
57+
scene = scene()
58+
59+
60+
main()
61+
pg.quit()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
import pandas as pd
4+
import tkinter as tk
5+
6+
# --- functions ---
7+
8+
def change(event, row, col):
9+
print(event.widget.get())
10+
df.iloc[row,col] = event.widget.get()
11+
print(df)
12+
13+
# --- main --
14+
15+
df = pd.DataFrame([[1,2,3], [5,6,7], [101,102,103]])
16+
17+
root = tk.Tk()
18+
19+
rows, cols = df.shape
20+
21+
for r in range(rows):
22+
for c in range(cols):
23+
e = tk.Entry(root)
24+
e.insert(0, df.iloc[r,c])
25+
e.grid(row=r, column=c)
26+
# ENTER
27+
#e.bind('<Return>', lambda event, y=r, x=c: change(event, y,x))
28+
# ENTER on keypad
29+
#e.bind('<Return>', lambda event, y=r, x=c: change(event, y,x))
30+
31+
e.bind('<KeyRelease>', lambda event, y=r, x=c: change(event, y,x))
32+
33+
root.mainloop()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
3+
import pandas as pd
4+
import tkinter as tk
5+
6+
class DataFrameEdit(tk.Frame):
7+
8+
def __init__(self, master, data, *args):
9+
super().__init__(master, *args)
10+
11+
self.data = data
12+
self.rows, self.cols = data.shape
13+
14+
for r in range(self.rows):
15+
for c in range(self.cols):
16+
e = tk.Entry(self)
17+
e.insert(0, self.data.iloc[r,c])
18+
e.grid(row=r, column=c)
19+
# ENTER
20+
#e.bind('<Return>', lambda event, y=r, x=c: self.change(event, y,x))
21+
# ENTER on keypad
22+
#e.bind('<Return>', lambda event, y=r, x=c: self.change(event, y,x))
23+
24+
e.bind('<KeyRelease>', lambda event, y=r, x=c: self.change(event, y,x))
25+
26+
def change(self, event, row, col):
27+
self.data.iloc[row,col] = event.widget.get()
28+
print(self.data)
29+
30+
31+
# --- main --
32+
33+
df1 = pd.DataFrame([[1,2,3], [5,6,7], [101,102,103]])
34+
df2 = pd.DataFrame([['a','x'], ['b','y'],['c','z']])
35+
36+
root = tk.Tk()
37+
38+
tk.Label(root, text='DataFrame #1').pack()
39+
DataFrameEdit(root, df1).pack()
40+
41+
tk.Label(root, text='DataFrame #2').pack()
42+
DataFrameEdit(root, df2).pack()
43+
44+
root.mainloop()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
It displays `DataFrame` in grid with `Entry`. You can change values in `Entry` and it will update data in `DataFrame` immediatelly.
3+
4+
![#1](images/screenshot.png?raw=true)
Loading

0 commit comments

Comments
 (0)