#!/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, Treeview 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) scrollX['troughcolor'] = scrollY['troughcolor'] = DARK_GRAY scrollX['bg'] = scrollY['bg'] = '#ddf' canvas.bind('', self.handle_canvas_resize) canvas.bind('', lambda event: canvas.scan_mark(event.x, event.y)) canvas.bind('', lambda event: canvas.scan_dragto(event.x, event.y, gain=7)) # 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) planets_list = self.planets_list = Treeview(star_system_frame, columns=('Name', 'BIO', 'IND')) planets_list.column('#0', width=10, minwidth=10, stretch=False) planets_list.column('Name', width=140, stretch=False) planets_list.column('BIO', width=50, stretch=False, anchor=CENTER) planets_list.column('IND', width=50, stretch=False, anchor=CENTER) planets_list.heading('Name', text='Name') planets_list.heading('BIO', text='BIO', anchor=CENTER) planets_list.heading('IND', text='IND', anchor=CENTER) planets_list.grid(row=3, 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): # I don't know why the Tk system doesn't update these values. self.canvas['width'] = event.width self.canvas['height'] = event.height # print(event) def update_star_system_tab(self, name, x, y): self.star_name_label['text'] = name self.star_x_label['text'] = f'x: {x}' self.star_y_label['text'] = f'y: {y}' self.planets_list.delete(*self.planets_list.get_children()) for values in stars.get_planets_for_star_named(data.conn, name): self.planets_list.insert('', END, values=values) 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, name in stars.iter_stars(data.conn): star_item_id = app.canvas.create_oval( x - radius, y - radius, x + radius, y + radius, fill='yellow', outline='yellow', activefill='#550', activeoutline='orange', activewidth=3, ) star_text_id = app.canvas.create_text( x, y + 20, text=name, fill='#BBB', activefill='#FFF', ) callback = ( lambda event, name=name, x=x, y=y: app.update_star_system_tab(name, x, y) ) app.canvas.tag_bind(star_item_id, '', callback) app.canvas.tag_bind(star_text_id, '', callback) ##app.frame.mainloop() ##data.close_db()