Kivy弹出窗口更改背景

2024-10-03 06:22:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么,但当我想改变我的弹出背景(我用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)

Tags: fromposimportselfsizedefclasspopup
2条回答

Popup中,您看到的大部分是Labels的背景。一个Labeltitle,另一个是你的ChooseBoxLabel。通过使用带有kv规则的自定义类为背景创建彩色Rectangle,可以轻松调整ChooseBoxLabel的背景色。{}{}更难,因为{}的开发人员没有任何方法访问{}背景色

以下是您可以做的一些事情的示例:

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时,该背景才可见。kv中的Label规则为titlechooseBoxLabel提供背景色,但它将影响App中的每个Label

事实上,我觉得这很简单。我的回答使问题复杂化了。我相信您只需添加以下行:

self.chosePop.opacity = 0.5

就在您创建Popup之后

相关问题 更多 >