This commit is contained in:
2022-10-28 19:50:09 +02:00
parent 8689704b6b
commit 9080e64d87
3 changed files with 146 additions and 0 deletions

BIN
icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.8 KiB

91
panel.py Normal file
View File

@ -0,0 +1,91 @@
from random import random
import wx
from slic.gui.widgets import LabeledMathEntry
from slic.gui.widgets import make_filled_hbox, make_filled_vbox
from slic.gui.widgets.plotting import PlotPanel
class MainPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.beans = []
self.spilled = []
btn_minus = wx.Button(self, label="-")
btn_plus = wx.Button(self, label="+")
btn_check = wx.Button(self, label="?")
btns = (btn_minus, btn_plus, btn_check)
hb_btns = make_filled_hbox(btns)
self.le_beans = le_beans = LabeledMathEntry(self, label="How many beans?", value=0)
self.le_spilled = le_spilled = LabeledMathEntry(self, label="Spilled beans:", value=0)
le_spilled.Disable()
self.plot_beans = plot_beans = PlotPanel(self)
self.plot_spilled = plot_spilled = PlotPanel(self)
plots = (plot_beans, plot_spilled)
hb_plots = make_filled_hbox(plots)
widgets = (hb_btns, le_beans, le_spilled, hb_plots)
box = make_filled_vbox(widgets, border=10)
self.SetSizerAndFit(box)
btn_minus.Bind(wx.EVT_BUTTON, self.on_click_minus)
btn_plus.Bind(wx.EVT_BUTTON, self.on_click_plus)
btn_check.Bind(wx.EVT_BUTTON, self.on_click_check)
def on_click_minus(self, _event):
le = self.le_beans
current = le.GetValue()
le.SetValue(current - 1)
self.update_plot()
def on_click_plus(self, _event):
if random() < 0.9:
le = self.le_beans
else:
le = self.le_spilled
current = le.GetValue() or 0
le.SetValue(current + 1)
self.update_plot()
def on_click_check(self, _event):
percent = self.get_percentage()
msg = f"You spilled {percent:.2}% of the beans."
self.le_beans.SetValue(0)
self.le_spilled.SetValue(0)
wx.MessageBox(msg, "Spillage Stats", wx.OK|wx.ICON_WARNING)
def get_percentage(self):
b = self.le_beans.GetValue() or 0
s = self.le_spilled.GetValue() or 0
return s / b * 100
def update_plot(self):
self.store_values()
self.draw_plot()
def store_values(self):
b = self.le_beans.GetValue() or 0
s = self.le_spilled.GetValue() or 0
self.beans.append(b)
self.spilled.append(s)
def draw_plot(self):
self.plot_beans.clear()
self.plot_spilled.clear()
self.plot_beans.plot(self.beans)
self.plot_spilled.plot(self.spilled)
self.plot_beans.draw()
self.plot_spilled.draw()

55
start.py Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python
import os
import wx
from slic.gui.persist import Persistence
from panel import MainPanel
def run(*args, **kwargs):
app = wx.App()
frame = MainFrame(*args, **kwargs)
frame.Show()
app.MainLoop()
app.Yield() #TODO: without this, wxPython segfaults locking a mutex
def get_wx_icon(fname="icon.png"):
iname = os.path.dirname(__file__)
iname = os.path.join(iname, fname)
return wx.Icon(iname)
class MainFrame(wx.Frame):
def __init__(self, title="Coffee 365"):
super().__init__(None, title=title)
self.SetIcon(get_wx_icon())
main = MainPanel(self)
# make sure the window is large enough
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(main, proportion=1, flag=wx.EXPAND)
self.SetSizerAndFit(sizer)
self.persist = persist = Persistence(".coffee365", self)
persist.load()
self.Bind(wx.EVT_CLOSE, self.on_close)
def on_close(self, event):
self.persist.save()
event.Skip() # forward the close event
if __name__ == "__main__":
run()