#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2024 Simon Forman
#
# This file is part of game
#
# game is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# game is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with game. If not see .
#
from tkinter import *
from tkinter.ttk import Notebook
import data, stars
DARK_GRAY = '#222'
class App:
'''
A canvas with scrolling support.
'''
def __init__(self, master=None, *canvas_args, **canvas_kw):
notebook = self.notebook = Notebook(master)
notebook.enable_traversal()
# Star Map
frame = self.frame = Frame(None, background='green')
# When putting a frame into a Notebook you use the add() method.
# but what should the parent of the frame be? The Notebook?
frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
# When putting a frame into a Notebook you evidently don't need
# to pack() it. Maybe because of the weights? I'm not setting
# the sticky arg to Notebook.add().
#frame.pack(expand=True, fill=BOTH)
canvas = self.canvas = Canvas(frame, *canvas_args, **canvas_kw)
scrollY = self.scrollY = Scrollbar(
frame,
orient=VERTICAL,
command=canvas.yview,
)
canvas['yscrollcommand'] = scrollY.set
scrollX = self.scrollX = Scrollbar(
frame,
orient=HORIZONTAL,
command=canvas.xview,
)
canvas['xscrollcommand'] = scrollX.set
canvas.grid(row=0, column=0, sticky=N+S+E+W)
scrollY.grid(row=0, column=1, sticky=N+S)
scrollX.grid(row=1, column=0, sticky=E+W)
canvas.bind("", self.handle_canvas_resize)
# Star System
star_system_frame = self.star_system_frame = Frame(None, bg=DARK_GRAY)
star_system_frame.columnconfigure(0, weight=1)
star_name_label = self.star_name_label = Label(star_system_frame, text='Star Name', bg='green', fg='white', anchor=W)
star_name_label.grid(row=0, column=0, sticky=E+W)
star_x_label = self.star_x_label = Label(star_system_frame, text='x', bg='green', fg='white', anchor=W)
star_x_label.grid(row=1, column=0, sticky=E+W)
star_y_label = self.star_y_label = Label(star_system_frame, text='y', bg='green', fg='white', anchor=W)
star_y_label.grid(row=2, column=0, sticky=E+W)
# Notebook Tabs
notebook.add(frame, text='Star Map', underline=5)
notebook.add(star_system_frame, text='Star System', underline=6)
self.star_system_tab_id = notebook.tabs()[-1]
notebook.pack(expand=True, fill=BOTH)
def handle_canvas_resize(self, event):
print(event)
def update_star_system_tab(self, star_id, x, y):
self.star_name_label['text'] = str(star_id)
self.star_x_label['text'] = f'x: {x}'
self.star_y_label['text'] = f'y: {y}'
self.notebook.select(self.star_system_tab_id)
data.open_db()
root = Tk()
app = App(root, bg='black', scrollregion=(0, 0, stars.WIDTH, stars.HEIGHT))
for x, y, radius in stars.iter_stars(data.conn):
star_id = app.canvas.create_oval(
x - radius, y - radius,
x + radius, y + radius,
fill='yellow',
outline='yellow',
activefill='#550',
activeoutline='orange',
activewidth=3,
)
app.canvas.tag_bind(
star_id,
'',
(lambda event, star_id=star_id, x=x, y=y:
app.update_star_system_tab(star_id, x, y)
)
)
##app.frame.mainloop()
##data.close_db()