· 6 years ago · Oct 11, 2019, 10:30 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 = cursor.fetchone()[0]
42 current_scene.delete(1.0, tk.END)
43 current_scene.insert(1.0, player_current_scene)
44
45
46master = tk.Tk()
47master.title("Tkinter Demo")
48master.geometry('800x600+100+50')
49header = tk.Label(master, text="Adventure Game Builder", font=("arial",30,"bold"), fg="steelblue").pack()
50
51
52scene_name_width = 40
53scene_name_height = 1
54scene_description_width = 40
55scene_description_height = 5
56current_scene_width = 40
57current_scene_height = 5
58
59tk.Label(master,
60 text="Enter scene name", font=("arial",12,"normal"), fg="grey").pack()
61scene_name = tk.Text(master, width=scene_name_width, height=scene_name_height, wrap=tk.WORD)
62scene_name.pack()
63
64tk.Label(master,
65 text="Enter scene description", font=("arial",12,"normal"), fg="grey").pack()
66
67scene_description = tk.Text(master, width=scene_description_width, height=scene_description_height, wrap=tk.WORD)
68scene_description.pack()
69
70
71tk.Button(master,
72 text='Save', command=add_to_db).pack()
73
74############ Current Scence Widget ################################
75
76tk.Label(master,
77 text="Current Scene", font=("arial",12,"normal"), fg="grey").pack()
78
79current_scene = tk.Text(master, width=current_scene_width, height=current_scene_height, wrap=tk.WORD)
80current_scene.pack()
81
82tk.Button(master,
83 text="Display current scene", command=display_current_scene).pack()
84
85##################################################################
86
87tk.Button(master,
88 text='Quit',
89 command=master.destroy).pack()
90
91tk.mainloop()
92conn.close()