gpt4 book ai didi

python - 创建名称中包含连续整数的多个按钮

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:32 24 4
gpt4 key购买 nike

我有以下 python 代码,我想知道是否可以在 for 循环中创建这些按钮?我正在考虑修改本地 namespace ,但我不确定这是否是个好主意。我真的想要命名按钮,以便连续命名。

self.todo1 = wx.TextCtrl(self, -1, "")
self.timer_label1 = wx.StaticText(self, -1, "00:00")
self.set_timer1 = wx.Button(self, -1, "Set Timer")
self.todo2 = wx.TextCtrl(self, -1, "")
self.timer_label2 = wx.StaticText(self, -1, "00:00")
self.set_timer2 = wx.Button(self, -1, "Set Timer")
self.todo3 = wx.TextCtrl(self, -1, "")
self.timer_label3 = wx.StaticText(self, -1, "00:00")
self.set_timer3 = wx.Button(self, -1, "Set Timer")
self.todo4 = wx.TextCtrl(self, -1, "")
self.timer_label4 = wx.StaticText(self, -1, "00:00")
self.set_timer4 = wx.Button(self, -1, "Set Timer")
self.todo5 = wx.TextCtrl(self, -1, "")
self.timer_label5 = wx.StaticText(self, -1, "00:00")
self.set_timer5 = wx.Button(self, -1, "Set Timer")

最佳答案

我认为内置的 setattr 方法可能是您最好的 friend 。这样的事情应该有效:

for i in range(1,6):
setattr(self,'todo%d' % i,wx.TextCtrl(self, -1, ""))
setattr(self,'timer_label%d' % i, wx.StaticText(self,-1,"00:00"))
setattr(self,'set_timer%d' % i, wx.Button(self,-1,"Set Timer"))

只要记住这样做:

object.x = y

与做的一样:

setattr(object,'x',y)

希望对您有所帮助!

关于python - 创建名称中包含连续整数的多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3231358/

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