· 6 years ago · Nov 06, 2019, 11:22 AM
1import sqlite3 as sq
2import tkinter as tk
3
4conn = sq.connect('demo.db')
5cursor = conn.cursor()
6
7# c.execute('DROP TABLE IF EXISTS "scenes";')
8
9cursor.execute("""
10 CREATE TABLE IF NOT EXISTS "scene" (
11 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
12 "scene_name" TEXT NOT NULL,
13 "scene_description" TEXT NOT NULL
14 );
15""")
16
17# CREATE TABLE "player" (
18 # "player_id" INTEGER PRIMARY KEY AUTOINCREMENT,
19 # "player_name" INTEGER NOT NULL,
20 # "player_current_scene" INTEGER NOT NULL,
21 # FOREIGN KEY("player_current_scene") REFERENCES "scene"("id")
22# );
23
24# INSERT INTO "main"."player"
25# ("player_name", "player_current_scene")
26# VALUES ('Jenny', 2);
27
28def add_to_db():
29 s = scene_name.get(1.0, tk.END).strip()
30 d = scene_description.get(1.0, tk.END).strip()
31 print(s, d)
32 cursor.execute('INSERT INTO scene ("scene_name", "scene_description") VALUES (?,?);', (s,d))
33 conn.commit()
34 scene_name.delete(1.0, tk.END)
35 scene_description.delete(1.0, tk.END)
36 scene_name.focus()
37
38def display_current_scene():
39
40 cursor.execute("SELECT current_scene FROM player WHERE player_name = (?);", ("Jenny",))
41 player_current_scene_id = cursor.fetchone()[0]
42 cursor.execute("SELECT scene_name FROM scene WHERE id = (?);", (player_current_scene_id,))
43 player_current_scene_description = cursor.fetchone()[0]
44 current_scene.delete(1.0, tk.END)
45 current_scene.insert(1.0, player_current_scene_description)
46
47
48master = tk.Tk()
49master.title("Tkinter Demo")
50master.geometry('800x600+100+50')
51header = tk.Label(master, text="Adventure Game Builder", font=("arial",30,"bold"), fg="steelblue").pack()
52
53
54scene_name_width = 40
55scene_name_height = 1
56scene_description_width = 40
57scene_description_height = 5
58current_scene_width = 40
59current_scene_height = 5
60
61tk.Label(master,
62 text="Enter scene name", font=("arial",12,"normal"), fg="grey").pack()
63scene_name = tk.Text(master, width=scene_name_width, height=scene_name_height, wrap=tk.WORD)
64scene_name.pack()
65
66tk.Label(master,
67 text="Enter scene description", font=("arial",12,"normal"), fg="grey").pack()
68
69scene_description = tk.Text(master, width=scene_description_width, height=scene_description_height, wrap=tk.WORD)
70scene_description.pack()
71
72
73tk.Button(master,
74 text='Save', command=add_to_db).pack()
75
76############ Current Scence Widget ################################
77
78tk.Label(master,
79 text="Current Scene", font=("arial",12,"normal"), fg="grey").pack()
80
81current_scene = tk.Text(master, width=current_scene_width, height=current_scene_height, wrap=tk.WORD)
82current_scene.pack()
83
84tk.Button(master,
85 text="Display current scene", command=display_current_scene).pack()
86
87##################################################################
88
89tk.Button(master,
90 text='Quit',
91 command=master.destroy).pack()
92
93tk.mainloop()
94conn.close()