[不确定为什么,但是当我想更改弹出窗口背景(我在python中创建,而不是在kivy中创建)时,我更改了整个屏幕的背景,但实际的弹出窗口除外。我的代码看起来像这样(分解很多):
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.window import Window
class BoxL(BoxLayout):
def chooseFile(self):
self.chosePop = Popup()
self.chosePop.title = 'My Popup'
choseBox = BoxLayout()
choseBoxLabel = Label()
choseBoxLabel.text = 'Any Text'
choseBox.add_widget(choseBoxLabel)
self.chosePop.content = choseBox
self.chosePop.background_normal = ''
self.chosePop.background_color = 0.5, 0.75, 0, 0.75
self.chosePop.open()
class GUI(App):
def build(self):
self.title = 'MyApp'
return BoxL()
if __name__ == '__main__':
GUI().run()
我也尝试过的是:
from kivy.graphics import Rectangle, Color
class BoxL(BoxLayout):
def chooseFile(self):
with self.chosePop.canvas:
Color(0, 0.5, 0.75, 0.75)
Rectangle(pos=choseBox.pos, size=choseBox.size)
#Rectangle(pos=self.chosePop.pos, size=self.chosePop.size) #this brings the correct size but at a wrong position, and the original popup background doesnt get changed either)
在Popup
中,您看到的大部分是Labels
的背景。一个Label
是title
,另一个是您的ChooseBoxLabel
。通过使用带有ChooseBoxLabel
规则的自定义类为背景创建彩色的kv
,可以轻松调整Rectangle
的背景颜色。title
Label
更加困难,因为Popup
的开发人员没有任何方法可以访问该title
背景色。
这是您可以做的一些事情的示例:
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.label import Label
class MyBoxLayout(BoxLayout):
pass
Builder.load_string('''
<Label>: # Note that this affects EVERY Label in the app
canvas.before:
Color:
rgba: 1,0,0,1
Rectangle:
pos: self.pos
size: self.size
<MyBoxLayout>:
canvas.before:
Color:
rgba: 0,0,1,1
Rectangle:
pos: self.pos
size: self.size
''')
class BoxL(BoxLayout):
def chooseFile(self):
self.chosePop = Popup()
self.chosePop.title = 'My Popup'
choseBox = MyBoxLayout()
choseBoxLabel = Label()
choseBoxLabel.size_hint_y = 0.2
choseBoxLabel.text = 'Any Text'
choseBox.add_widget(choseBoxLabel)
self.chosePop.content = choseBox
self.chosePop.size_hint = (.5, .5)
self.chosePop.open()
class GUI(App):
def build(self):
self.title = 'MyApp'
Clock.schedule_once(self.do_popup, 3)
return BoxL()
def do_popup(self, dt):
self.root.chooseFile()
if __name__ == '__main__':
GUI().run()
在上面的代码中,MyBoxLayout
定制类提供了蓝色背景,仅当其中的Label
未填充Layout
时,该背景才可见。 Label
中的kv
规则为title
和chooseBoxLabel
提供了背景色,但会影响Label
中的每个App
。