我使用的是Python 3.6.1,我遇到了一些非常奇怪的东西.我有一个简单的字典分配拼写错误,花了我很长时间才找到.
context = {}
context["a"]: 2
print(context)
Run Code Online (Sandbox Code Playgroud)
产量
{}
Run Code Online (Sandbox Code Playgroud)
代码context["a"]: 2在做什么?它不会引起SyntaxError它应该IMO的时间.起初我以为它创造了一个切片.然而,打字repr(context["a"]: 2)提出了一个SyntaxError.我也输入context["a"]: 2了控制台,控制台没有打印任何东西.我想也许它回来了None,但我不太确定.
我也认为它可能是一行if语句,但这也不应该是正确的语法.
另外,context["a"]应该提高一个KeyError.
我很困惑.到底是怎么回事?
kivy语言可以访问继承的布局和小部件吗?我想创建一个基本的BoxLayout,其中包含我的小部件的样式和标题标签.我希望能够从这个小部件继承并在不同的位置添加其他小部件.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<SimpleBar>:
canvas.before:
Color:
rgba: 0, 0.5, 0.5, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
id: my_layout
Label:
text: "hi"
<NewBar>:
Label:
text: "2"
''')
class SimpleBar(BoxLayout):
def log(self, value):
print(value)
class NewBar(SimpleBar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
print(dir(self))
class GeneralApp(App):
def build(self):
return NewBar()
if __name__ == '__main__':
GeneralApp().run()
Run Code Online (Sandbox Code Playgroud)
以上是我的基本运行小部件.
我希望NewBar的"2"标签位于SimpleBar的'hi'标签之前,如下所示.
<NewBar>:
BoxLayout:
id: my_layout
Label:
text: "2"
Label:
text: "hi"
Run Code Online (Sandbox Code Playgroud)
我知道 - 可以否定物品.但是,<-NewBar> …