python - What is the most pythonic way to pass UI data to a `tkinter` GUI? -
as way teach myself python, have dusted off of old undergraduate mechanical engineering texts , creating calculators simple concepts. have calculator class (very simple brevity):
from math import pi import tkinter tk class cylinder(object): """abstraction of cylinder primitive.""" def __init__(self): """see class docstring.""" self.radius = 0.0 self.height = 0.0 def calc_volume(self): """see class docstring.""" self.volume = pi * self.height * self.radius**2
and want wrap in tkinter
gui. there potentially large number of calculators in project , don't want code custom wrappers each calculator, trying develop content agnostic approach. so, started writing:
class calcgui(tk.frame): """to do. write me.""" def __init__(self, master=none, calc_object=none): """see class docstring.""" tk.frame.__init__(self, master) self.grid() self.entries, self.entry_vars = {}, {} num, item in enumerate(calc_object.__dict__): tk.label(self, text=item).grid(row=num, column=0) self.entry_vars[item] = tk.stringvar() self.entries[item] = tk.entry(self, textvariable=self.entry_vars[item]) self.entries[item].grid(row=num, column=1)
this works fine simple variable names radius
, more complicated items, don't want muddle calculators long variable names self.coefficient_of_thermal_expansion
or self.average_air_velocity_of_a_coconut_laden_swallow
, elegant way pass consider ui information gui, english name, expected units, etc. , looking best practices. options i've considered:
- make of variables
tuples
ornamedtuples
slots english name, value , units.- i think complicate calculation logic , reduce readability.
- create class level dict containing of ui config info calculator class.
- this keeps readability within calculator class, means storing things in class aren't used within class (seems unpythonic me).
- create separate config module settings everything.
- this keeps calculator classes nice , tidy, may reduce readability inside gui module.
which of these (if any!) considered best practice within python?
edit why not long variables? consider following equation beam deflection:
y = (3 * - x) * (p * x**2) / (6 * e * i)
putting in class method write like:
self.y = (3 * self.a - self.x) * (self.p * self.x**2) / (6 * self.e * self.i)
or long variables:
self.beam_deflection = ((3 * self.force_location - self.location_on_beam) * (self.point_force * self.location_on_beam**2) / (6 * self.modulus_of_elasticity * self.moment_of_inertia))
i think first option more readable. said, there no reason not alias vars like: e = self.modulus_of_elasticity
but still solves 1 piece of puzzle. doesn't address units or other items (say flag whether variable should entered entry or listbox, etc).
Comments
Post a Comment