Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate blocks of Python code with for loop
    primarykey
    data
    text
    <p>I made a small GUI program with PyGtk for parametrically drawing the frame of a bike (the idea: you move some slider, and the frame gets redrawn with the updated parameter).</p> <p>Somewhere in the code, I must create a slider for each parameter, and I get this very non-pythonic and repetitive code (sorry, I could not get the indentation right while writing this message):</p> <pre><code>def adjustbottomBracketHeight(par): BikeDrawing.p.bottomBracketHeight = par.get_value() painelhoriz.queue_draw() bottomBracketHeightAdjust = gtk.Adjustment(value=BikeDrawing.p.bottomBracketHeight, lower=180., upper=400., step_incr=10.) bbhScale = gtk.HScale(adjustment=bottomBracketHeightAdjust) bbhScale.set_value_pos(gtk.POS_LEFT) bbhScale.connect("value-changed", adjustbottomBracketHeight) bbhLabel = gtk.Label("Bottom bracket height") topcolumn1.pack_start(bbhLabel, False, False) topcolumn1.pack_start(bbhScale, True, True) def adjustseatTubeAngle(par): BikeDrawing.p.seatTubeAngle = par.get_value() painelhoriz.queue_draw() seatTubeAngleAdjust = gtk.Adjustment(value=BikeDrawing.p.seatTubeAngle, lower=60., upper=85., step_incr=0.5) staScale = gtk.HScale(adjustment=seatTubeAngleAdjust) staScale.set_value_pos(gtk.POS_LEFT) staScale.connect("value-changed", adjustseatTubeAngle) staLabel = gtk.Label("Seat tube angle") topcolumn1.pack_start(staLabel, False, False) topcolumn1.pack_start(staScale, True, True) def adjustSeatTubeLength(par): BikeDrawing.p.seatTubeLength = par.get_value() painelhoriz.queue_draw() seatTubeLengthAdjust = gtk.Adjustment(value=BikeDrawing.p.seatTubeLength, lower=300., upper=700., step_incr=10.) stlScale = gtk.HScale(adjustment=seatTubeLengthAdjust) stlScale.set_value_pos(gtk.POS_LEFT) stlScale.connect("value-changed", adjustSeatTubeLength) topcolumn1.pack_start(stlScale, True, True) def adjusttopTubeLength(par): BikeDrawing.p.topTubeLength = par.get_value() painelhoriz.queue_draw() topTubeLengthAdjust = gtk.Adjustment(value=BikeDrawing.p.topTubeLength, lower=400., upper=700., step_incr=10.) ttlScale = gtk.HScale(adjustment=topTubeLengthAdjust) ttlScale.set_value_pos(gtk.POS_LEFT) ttlScale.connect("value-changed", adjusttopTubeLength) topcolumn1.pack_start(ttlScale, True, True) </code></pre> <p>Well with some minor adaptations, I would like (to know how ;o) to "create" this kind of code iterating over a list of the parameter names, something similar to:</p> <pre><code>par_list = ['bottomBracketHeight', 'seatTubeAngle', 'seatTubeHeight'] def widgetize(PARAMETER): """ Create blocks of code where the name PARAMETER would be used to personalize names of handlers, functions, objects, etc. """ for par in par_list: widgetize(par) </code></pre> <p>I read some previous questions/answers and there seems not to be an answer to this specific problem.</p> <p>I appreciate very much your attention</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload