game/ui.py

60 lines
1.4 KiB
Python

from fractions import Fraction as ratio
from tkinter import *
from PIL import Image
from PIL.ImageTk import PhotoImage
from data import load
class App:
'''
A canvas with scrolling support.
'''
def __init__(self, master=None, *canvas_args, **canvas_kw):
frame = self.frame = Frame(root, bg='green')
frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
C = self.canvas = Canvas(frame, *canvas_args, **canvas_kw)
scrollY = self.scrollY = Scrollbar(
frame,
orient=VERTICAL,
command=C.yview,
)
C['yscrollcommand'] = scrollY.set
scrollX = self.scrollX = Scrollbar(
frame,
orient=HORIZONTAL,
command=C.xview,
)
C['xscrollcommand'] = scrollX.set
C.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)
C.bind("<Configure>", self.handle_canvas_resize)
def handle_canvas_resize(self, event):
print(event)
state = load('game.state')
width, height = state['width'], state['height']
root = Tk()
app = App(root, bg='black', scrollregion=(0, 0, width, height))
app.frame.pack(expand=True, fill=BOTH)
for (x, y), radius in state['stars'].items():
app.canvas.create_oval(
x - radius, y - radius,
x + radius, y + radius,
fill='yellow',
)