gpt4 book ai didi

python - 使用 for 循环生成 Python 代码块

转载 作者:行者123 更新时间:2023-11-28 20:11:06 24 4
gpt4 key购买 nike

我用 PyGtk 制作了一个小型 GUI 程序,用于参数化地绘制自行车车架(想法:移动一些 slider ,然后使用更新后的参数重新绘制车架)。

在代码的某处,我必须为每个参数创建一个 slider ,我得到了这段非常非 pythonic 和重复的代码(抱歉,我在写这条消息时无法正确缩进):

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)

好吧,通过一些小的改编,我想(知道如何 ;o)“创建”这种遍历参数名称列表的代码,类似于:

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)

我阅读了一些以前的问题/答案,但似乎没有针对这个特定问题的答案。

非常感谢您的关注

最佳答案

我认为这或多或少是您所追求的:

def makeAdjuster(name, attr, lower, upper, step):
def doAdjust(par):
setattr(Bikedrawing.p, attr, par.get_value())
panelhoriz.queue_draw()

val = getattr(Bikedrawing.p, attr)
adjust = gtk.Adjustment(value=val, lower=lower, upper=upper, step_incr=step)

label = gtk.Label(name)
topcolumn1.pack_start(label, False, False)

scale = gtk.HScale(adjustment=adjust)
scale.set_value_pos(gtk.POS_LEFT)
scale.connect("value-changed", doAdjust)
topcolumn1.pack_start(scale, True, True)

for adj in (
('Bottom bracket height', 'bottomBracketHeight', 180., 400., 10.),
('Seat tube angle', 'seatTubeAngle', 60., 80., 0.5),
('Seat tube length', 'seatTubeLength', 300., 700., 10.),
('Top tube length', 'topTubeLength', 400., 700., 10.)
):
makeAdjuster(*adj)

关于python - 使用 for 循环生成 Python 代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5080756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com