马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 Aaron栩生阿龙 于 2026-1-25 20:17 编辑
我们亲爱的烈烈曾经说过:我说gallery是renpy的屎山代码,有没有懂的)
他说的对。
---分割线---
不过我的问题是:如何优雅地获取Gallery中名为name的button所对应的images?
目前倒是有方法获取,但是不太美观,我在浏览了10086遍00gallery.rpy文件后还没发现比较好的方法,所以来这里问一下,如果没有其他解决方式的话我就把这篇帖子改成经验教程吧(
先贴上代码吧
[RenPy] 纯文本查看 复制代码
Gallery().buttons[name].images[0].displayables[0]
然后我们来说一下具体原理
查阅源文件我们可以得知,Gallery对象有一个名为buttons的字典,其中键是button的name,值是button本身(原文为A map from button name (or image) to __GalleryButton object.)
这个button呢又是一个__GalleryButton对象,
[Python] 纯文本查看 复制代码
class __GalleryButton(object):
def __init__(self, gallery, index):
self.gallery = gallery
self.images = [ ] # 没错就是这个东西
self.conditions = [ ]
self.index = index
它有一个名为images的属性,是一个列表,其中的元素是该button对应的image
但是——这里的image不是一个可视组件,它是一个__GalleryImage对象,
[Python] 纯文本查看 复制代码
class __GalleryImage(object):
show_properties = None
def __init__(self, gallery, displayables, **properties):
self.gallery = gallery
self.conditions = [ ]
self.displayables = displayables # 没错是这个东西
self.transforms = [ None ] * len(displayables)
self.show_properties, = renpy.split_properties(properties, "show_")
这个对象有一个名为displayables的属性,是一个元组,其中的元素就是可视组件了,于是我们终于获得到了这个可视组件
所以上面的代码获取的是名为name的button所对应的第一个image,如果需要获得全部的image需要循环images列表,并且循环出来之后还要循环displayables这个元组,最终获得的元素才是可视组件本身
所以,真的没有优雅一点的方法吗,虽然确实可以自己写一个函数循环就是了
|