【发布时间】:2018-06-11 14:51:40
【问题描述】:
我是 python 的初学者,正在尝试使用 Kivy 制作 GUI。 要为布局添加背景,我正在尝试按照官方文档中提供的示例进行操作。
(请参考this Please,如果您需要查看官方文档)
https://kivy.org/docs/guide/widgets.html#adding-a-background-to-a-layout
在下面的代码 update_bg() 中,该函数用于在其父级(“布局”,Kivy 行话)更改其背景时更新背景(绘制在画布上的矩形)的大小和/或位置位置和/或大小。
class ConversationBox(BoxLayout):
def __init__(self, **kwargs):
super(ConversationBox, self).__init__(**kwargs)
beside_message = BoxLayout(orientation='vertical')
whatever_said = Label(text='Someone said Something', size_hint=(None, None), size=(100, 60))
remove_button = Button(text='X', size_hint=(None, None), size=(30, 30))
log = Label(text='Log', size_hint=(None, None), size=(30, 30))
with self.canvas:
Color(0, 1, 0, 1)
self.background = Rectangle(pos_hint=(None, None), size_hint=(None, None), pos=self.pos, size=self.size)
self.bind(pos=self.update_bg, size=self.update_bg)
self.add_widget(whatever_said)
beside_message.add_widget(remove_button)
beside_message.add_widget(log)
self.add_widget(beside_message)
def update_bg(self): # <----------------This is where the problem is
self.background.pos = self.pos
self.background.size = self.size
class test(App):
def build(self):
return ConversationBox(orientation='horizontal')
test().run()
当你运行这段代码时,你会在控制台中报错,也就是说。
TypeError: update_bg() 接受 1 个位置参数,但给出了 3 个
当你提供两个额外的参数时,比如说,
def update_bg(self, arbitrary_arg_1, arbitrary_arg_2):
您不会收到任何错误。 为什么会这样? 我的直觉为零。
【问题讨论】: