Skip to content

Instantly share code, notes, and snippets.

@arkidas
Created May 6, 2012 01:57
Show Gist options
  • Save arkidas/2607014 to your computer and use it in GitHub Desktop.
Save arkidas/2607014 to your computer and use it in GitHub Desktop.
Reiknivél fyrir nammibarinn í Hagkaup.
# -*- coding: utf-8 -*-
import wx
import datetime
class Candulator(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='Nammireiknir')
#Initialize variables
global total_price, total_weight, total_calories, today, current_selection
total_price = 0
total_weight = 0
total_calories = 0
today = datetime.date.today() #This is to check whether today is Saturday (half price day)
current_selection = 'Sur broskall'
#Data table with candy types
#Price, Weight, Calories, Index (for the counter)
global candy_data
candy_data = {'Sur broskall' : [5,1,7,0],'Storar mondlur' : [8,2,9,1], 'Stor Haribosnud' : [10,4,9,2]}
#Counter to keep track of how much you have of each type of candy
global candy_count, total_candy_count
candy_count = [0] * 10
total_candy_count = 0
# Add a panel so it looks correct on all platforms
self.panel = wx.Panel(self, wx.ID_ANY)
self.Centre()
#Combobox
candy_types = []
for key in candy_data: #Populate candy_types with a For Loop.
candy_types.append(key)
candy_list = wx.ComboBox(self.panel, value='Sur broskall', choices=candy_types, style=wx.CB_READONLY)
#Labels
global label_price, label_weight, label_calories
label_price = wx.StaticText(self.panel, wx.ID_ANY, 'Verð: ' + str(total_price) + ' kr')
label_weight = wx.StaticText(self.panel, wx.ID_ANY, 'Þyngd: ' + str(total_price) + ' gr')
label_calories = wx.StaticText(self.panel, wx.ID_ANY, 'Kaloríur: ' + str(total_calories) + ' kcal')
#Buttons
btn_show_contents = wx.Button(self.panel, wx.ID_ANY, 'Sýna innihald')
btn_add = wx.Button(self.panel, wx.ID_ANY, 'Bæta við')
btn_remove = wx.Button(self.panel, wx.ID_ANY, 'Fjarlægja')
#Bind events to the buttons
self.Bind(wx.EVT_BUTTON, self.on_add, btn_add)
self.Bind(wx.EVT_BUTTON, self.on_show_contents, btn_show_contents)
self.Bind(wx.EVT_BUTTON, self.on_remove, btn_remove)
self.Bind(wx.EVT_COMBOBOX, self.on_select)
#Declare and set variables for the sizers
sz_primary = wx.BoxSizer(wx.VERTICAL)
sz_price = wx.BoxSizer(wx.HORIZONTAL)
sz_weight = wx.BoxSizer(wx.HORIZONTAL)
sz_cals = wx.BoxSizer(wx.HORIZONTAL)
#Add text labels to their respective sizers
sz_price.Add(label_price, 0)
sz_cals.Add(label_calories, 0)
sz_weight.Add(label_weight, 0)
#Add all sub-sizers to the primary sizer
sz_primary.Add(sz_price, 0, wx.ALL|wx.EXPAND,5)
sz_primary.Add(sz_cals, 0, wx.ALL|wx.EXPAND, 5)
sz_primary.Add(sz_weight, 0, wx.ALL|wx.EXPAND, 5)
sz_primary.Add(candy_list, 0, wx.ALL|wx.EXPAND, 5)
sz_primary.Add(btn_add, 0, wx.ALL|wx.EXPAND, 5)
sz_primary.Add(btn_show_contents, 0, wx.ALL|wx.EXPAND, 5)
sz_primary.Add(btn_remove, 0, wx.ALL|wx.EXPAND, 5)
#Define which sizer to use as primary
self.panel.SetSizer(sz_primary)
#Window size resized to fit contents of primary sizer
sz_primary.Fit(self)
#Event handlers
def on_add(self, e):
global total_price, total_weight, total_calories, candy_count, current_selection, today, total_candy_count
total_price = total_price + candy_data[current_selection][0]
total_weight = total_weight + candy_data[current_selection][1]
total_calories = total_calories + candy_data[current_selection][2]
#If it is Saturday, display half price. Else display full price.
if today.weekday() == 5:
label_price.SetLabel(label='Verð: ' + str(total_price*0.5) + ' kr')
else:
label_price.SetLabel(label='Verð: ' + str(total_price) + ' kr')
label_weight.SetLabel(label='Þyngd: ' + str(total_weight) + ' gr')
label_calories.SetLabel(label='Kaloríur: ' + str(total_calories) + ' kcal')
candy_count[candy_data[current_selection][3]] += 1
total_candy_count += 1
def on_select(self, e):
#We define the currently selected candy as a global
#variable so that it can be passed to the 'on_add' method.
global current_selection
current_selection = e.GetString()
def on_remove(self, e):
global total_price, total_weight, total_calories, candy_count, current_selection, today, total_candy_count
if candy_count[candy_data[current_selection][3]] == 0:
wx.MessageBox('Það er ekkert eftir af þessu nammi í nammipokanum!', 'Villa!',
wx.OK | wx.ICON_EXCLAMATION)
else:
total_price = total_price - candy_data[current_selection][0]
total_weight = total_weight - candy_data[current_selection][1]
total_calories = total_calories - candy_data[current_selection][2]
#If it is Saturday, display half price. Else display full price.
if today.weekday() == 5:
label_price.SetLabel(label='Verð: ' + str(total_price*0.5) + ' kr')
else:
label_price.SetLabel(label='Verð: ' + str(total_price) + ' kr')
label_weight.SetLabel(label='Þyngd: ' + str(total_weight) + ' gr')
label_calories.SetLabel(label='Kaloríur: ' + str(total_calories) + ' kcal')
candy_count[candy_data[current_selection][3]] -= 1
total_candy_count -= 1
def on_show_contents(self, e):
if total_candy_count == 0:
wx.MessageBox('Það er ekkert í nammipokanum!', 'Villa!',
wx.OK | wx.ICON_EXCLAMATION)
else:
candybag_contents = ''
for key in candy_data:
if candy_count[candy_data[key][3]] != 0:
candybag_contents += str(candy_count[candy_data[key][3]]) + 'x ' + key + '\n'
wx.MessageBox(candybag_contents, 'Innihald nammipokans',
wx.OK | wx.ICON_INFORMATION)
# Run the program
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = Candulator().Show()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment