全窗口覆盖工具

0 投票
1 回答
46 浏览
提问于 2025-04-14 16:36

我有两个问题需要解决:

  • 有没有什么工具可以让我的图片完全覆盖我打开的窗口?我的图片大小是1368x768。
  • 我虽然在代码的第12行指定了深色模式,但我的窗口却没有变成深色模式。

这两个问题怎么解决呢?

https://imgur.com/ootQu5P

https://imgur.com/8oPBK5B

任何帮助都非常感谢!

1 个回答

0

对于第一个问题,你可以简单地修改kv文件的内容,像这样:

<testApp>:
    Screen:
        Image:
            source: "pic.png"
            size_hint: 1, 1
            allow_stretch: True
            keep_ratio: False

这样做会让它自动调整大小以适应整个窗口,并默认放在左上角(如果你不需要特别的位置,就不需要指定pos_hint)。请注意,你的文件中有一些拼写错误,比如把'halgin'写成了'halign'。

另外,你也可以使用更新后的语法,像这样:

fit_mode: "fill"

这个语句在KivyMD的文档中有说明:

To control how the image should be adjusted to fit inside the widget box, you should use the fit_mode property. Available options include:

"scale-down": maintains aspect ratio without stretching.

"fill": stretches to fill widget, may cause distortion.

"contain": maintains aspect ratio and resizes to fit inside widget.

"cover": maintains aspect ratio and stretches to fill widget, may clip

关于第二个问题,你需要修改theme_style而不是theme__style。所以,正确的写法是:

self.theme_cls.theme_style = 'Dark'

原因是:这是KivyMD框架中约定俗成的属性名称。想了解更多信息,可以参考它的文档。

我已经修改了你的程序,以下是完整的内容:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen


class testApp(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


class MyApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = 'Dark'
        return testApp()
    def change_theme(self):
        if self.theme_cls.theme_style == 'Dark':
            self.theme_cls.theme_style = 'Light'
        else:
            self.theme_cls.theme_style = 'Dark'


if __name__ == '__main__':
    Window.size = (1100, 600)
    Builder.load_file("main.kv")
    MyApp().run()
<testApp>:
    Screen:
        Image:
            source: "pic.png"
            size_hint: 1, 1
            fit_mode: "fill"

        MDLabel:
            text: "cover the window I open"
            font_size: "48dp"
            halign: "center"
            color: app.theme_cls.text_color
            pos_hint: {'center_x': 0.4, 'center_y': 0.4}
        MDRaisedButton:
            text: "Click me!"
            pos_hint: {'center_x': 0.5, 'center_y': 0.2}
            on_press: app.change_theme()
            md_bg_color: app.theme_cls.primary_color
            text_color: app.theme_cls.text_color

请注意,我添加了一个简单的MDRaisedButton来显示主题激活的状态。通常情况下,你应该能看到文本颜色的变化,这表示主题正常工作。

希望这能帮到你!

撰写回答