minor cleanup, mess with bind tags

This commit is contained in:
Simon Forman 2024-04-12 19:39:09 -07:00
parent effc940235
commit be55cc5432
3 changed files with 11 additions and 9 deletions

View File

@ -39,6 +39,7 @@ def initialize_db_tables(filename):
def close_db(): def close_db():
global conn
if conn: if conn:
conn.close() conn.close()
conn = None conn = None
@ -54,11 +55,6 @@ def open_db(filename=FILENAME):
return conn return conn
def generate_stars(width, height, minimum_distance_between_stars):
for x, y in poisson(width, height, minimum_distance_between_stars):
yield x, y, round(1 + expovariate(1))
def main(filename): def main(filename):
print('Initializing db file', filename) print('Initializing db file', filename)
conn = initialize_db_tables(filename) conn = initialize_db_tables(filename)

View File

@ -18,10 +18,11 @@
# along with game. If not see <http://www.gnu.org/licenses/>. # along with game. If not see <http://www.gnu.org/licenses/>.
# #
from random import randint, expovariate, seed from random import randint, expovariate, seed
import sqlite3
from poisson import poisson from poisson import poisson
seed(23)
MINIMUM_DISTANCE_BETWEEN_STARS = 160 MINIMUM_DISTANCE_BETWEEN_STARS = 160
WIDTH, HEIGHT = 10240, 7680 WIDTH, HEIGHT = 10240, 7680
@ -38,7 +39,6 @@ def generate_stars(width, height, minimum_distance_between_stars):
def init_db(conn): def init_db(conn):
print('Generating star data.') print('Generating star data.')
seed(23)
c = conn.cursor() c = conn.cursor()
for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS): for values in generate_stars(WIDTH, HEIGHT, MINIMUM_DISTANCE_BETWEEN_STARS):
c.execute('insert into stars values (?, ?, ?)', values) c.execute('insert into stars values (?, ?, ?)', values)

10
ui.py
View File

@ -32,6 +32,7 @@ class App:
frame = self.frame = Frame(root, bg='green') frame = self.frame = Frame(root, bg='green')
frame.rowconfigure(0, weight=1) frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1) frame.columnconfigure(0, weight=1)
frame.pack(expand=True, fill=BOTH)
C = self.canvas = Canvas(frame, *canvas_args, **canvas_kw) C = self.canvas = Canvas(frame, *canvas_args, **canvas_kw)
@ -63,13 +64,18 @@ data.open_db()
root = Tk() root = Tk()
app = App(root, bg='black', scrollregion=(0, 0, stars.WIDTH, stars.HEIGHT)) app = App(root, bg='black', scrollregion=(0, 0, stars.WIDTH, stars.HEIGHT))
app.frame.pack(expand=True, fill=BOTH)
for x, y, radius in stars.iter_stars(data.conn): for x, y, radius in stars.iter_stars(data.conn):
app.canvas.create_oval( star_id = app.canvas.create_oval(
x - radius, y - radius, x - radius, y - radius,
x + radius, y + radius, x + radius, y + radius,
fill='yellow', fill='yellow',
outline='yellow',
activefill='#550',
activeoutline='orange',
activewidth=3,
) )
app.canvas.tag_bind(star_id, '<Enter>', (lambda event, sid=star_id, x=x, y=y: root.title(f'{x}, {y}')))
app.frame.mainloop() app.frame.mainloop()
data.close_db()