Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 20 17:18:39 2018
@author: Dimitar Dimov
"""
import PIL
import Tkinter
import Tkinter as Tk
from math import *
from PIL import ImageTk, Image
from collections import OrderedDict, namedtuple
from itertools import chain
BuildingOption = namedtuple('BuildingOption', ['name', 'cost'])
TickBoxTuple = namedtuple('TickBoxTuple', ['var', 'tickbox'])
BUILDING_TYPES = OrderedDict([
('house', {
'cost': 40
}),
('5 Storey Building', {
'cost': 50,
'options': [
BuildingOption('P-M Plant Rooms', 1),
BuildingOption('P-M Risers', 1),
BuildingOption("P-M HIU's", 1),
BuildingOption('P-M SIPS Facades', 1),
BuildingOption('Large Format Facade Materials', 1),
],
}),
('10 Storey Building', {
'cost': 60,
}),
('15 Storey Building', {
'cost': 60,
}),
])
GENERAL_CHOICES = [
BuildingOption('P-M Foundations', 1),
BuildingOption('P-M Frame Elements', 1),
BuildingOption('P-M Roof Solutions', 1),
BuildingOption('P-M Internal Walls', 1),
BuildingOption('P-M Doorsets', 1),
BuildingOption('P-M Bathrooms/ Kitchens', 1),
BuildingOption('P-M Wiring Solutions', 1),
]
CONSTRUCTION_CHOICES = {
'Traditional': [],
'2-D': [
BuildingOption('P-M Structural Systems including Insulation:', 1),
BuildingOption('P-M Facade Solution', 1),
],
'3-D': [
BuildingOption('P-M Core', 1),
BuildingOption('P-M Integrated Modular Facade', 1),
BuildingOption('P-M Common Areas (Unfurnished)', 1),
BuildingOption('P-M Common Areas (Furnished)', 1),
],
}
def get_building_options(building_type):
return BUILDING_TYPES[building_type.get()].get('options', [])
def get_construction_options(construction_choice):
return CONSTRUCTION_CHOICES[construction_choice.get()]
class pmvCalculator(Tk.Frame):
def __init__(self, parent, *args):
Tk.Frame.__init__(self, parent, *args)
self.parent = parent
self.main = Tk.Frame(parent)
self.main.grid(row=0, column=0)
self.tickboxes = {}
self.grid()
self.logo = self.insert_logo()
self.logo.grid(row=1, column=2, columnspan=4, rowspan=3, padx=50, pady=15)
self.option_button = Tk.Button(self, text='Show Options', command=self.show_options)
self.option_button.grid(row=13, column=2, pady=10)
self.dropdown_frame, self.building_type, self.construction_choice = self.populate_dropdown()
self.pmv_calc_picture = self.add_pmv_calc_picture()
self.pmv_calc_picture.grid(row=14, column=6, columnspan=3, rowspan=30, padx=20, pady=15)
def insert_logo(self):
logoFrame = Tk.Frame(self)
path = PIL.Image.open("cast.gif.png")
img = PIL.ImageTk.PhotoImage(path)
return Tk.Label(self, image=img)
def populate_dropdown(self):
dropdown_frame = Tk.Frame(self)
building_type = Tk.StringVar(self)
construction_choice = Tk.StringVar(self)
Tk.Label(self, text='Enter your preferred type of building: ').grid(row=9, column=2, sticky=Tk.W)
Tk.OptionMenu(self, building_type, *BUILDING_TYPES.keys()).grid(row=9, column=3)
Tk.Label(self, text='Enter your preferred type of procurement: ').grid(row=11, column=2, sticky=Tk.W)
Tk.OptionMenu(self, construction_choice, *CONSTRUCTION_CHOICES.keys()).grid(row=11, column=3)
return dropdown_frame, building_type, construction_choice
def show_options(self):
self.populate_tickboxes()
Tk.Button(self, text='Calculate', command=self.calculate_cost).grid(row=33, column=2, pady=10, sticky=Tk.E)
def clear_tickboxes(self):
while self.tickboxes:
_, (tickbox) = self.tickboxes.popitem()
tickbox.tickbox.destroy()
def populate_tickboxes(self):
self.clear_tickboxes()
tickboxes = (
GENERAL_CHOICES
+ get_building_options(self.building_type)
+ get_construction_options(self.construction_choice)
)
for row, option in enumerate(tickboxes, 15):
self.tickboxes[option] = self.make_tickbox(option.name, row)
def calculate_cost(self):
total_cost = sum(tickbox.var.get() * option.cost for (option, tickbox) in self.tickboxes.items())
total_cost += BUILDING_TYPES[self.building_type.get()]['cost']
Tk.Label(self, text='{} %'.format(total_cost)).grid(row=33, column=3, pady=10)
return total_cost
def add_pmv_calc_picture(self):
path = PIL.Image.open("pmv_calc.png")
path = path.resize((600, 300), Image.ANTIALIAS)
path.save('update_pm_image.ppm', 'ppm')
img = PIL.ImageTk.PhotoImage(path)
return Tk.Label(self, image=img)
def make_tickbox(self, label, newrow):
var = Tk.IntVar()
tickbox = Tk.Checkbutton(self, text=label, variable=var)
tickbox.grid(row=newrow, column=2, sticky=Tk.W)
return TickBoxTuple(var, tickbox)
if __name__ == '__main__':
root = Tk.Tk()
root.title('Cast PMV Calculator')
root.geometry('1100x800')
app = pmvCalculator(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.