You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kivy应用中禁用最小化最大化按钮并设置固定窗口大小

Kivy窗口配置需求实现方案

1. 窗口完全固定不可调整尺寸实现

你当前使用的代码已经可以实现固定窗口尺寸的效果:

Window.size = (800, 600)
Window.minimum_width, Window.minimum_height = Window.size
Window.maximum_width, Window.maximum_height = Window.size

上述代码通过限制窗口的最小、最大尺寸和初始尺寸完全一致,从逻辑上阻止了窗口尺寸调整。如果要从底层彻底禁用调整尺寸的能力,可以搭配修改窗口原生属性的方案,和禁用最小化最大化按钮的逻辑一同实现。

2. 禁用窗口最小化、最大化按钮实现

Kivy没有直接封装窗口按钮控制的接口,需要调用默认SDL2后端的原生接口实现,完整可运行的代码如下:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.utils import platform
import sdl2

# 配置固定窗口尺寸
Window.size = (800, 600)
Window.minimum_width, Window.minimum_height = Window.size
Window.maximum_width, Window.maximum_height = Window.size

# 桌面端窗口属性修改逻辑,移动端不需要执行
if platform not in ["android", "ios"]:
    # 获取SDL原生窗口指针
    sdl_window = sdl2.SDL_GetWindowFromID(Window.get_window_id())
    # 获取当前窗口属性标志
    current_flags = sdl2.SDL_GetWindowFlags(sdl_window)
    # 移除可最大化、可最小化、可调整尺寸的属性标志
    new_flags = current_flags & ~sdl2.SDL_WINDOW_MAXIMIZABLE & ~sdl2.SDL_WINDOW_MINIMIZABLE & ~sdl2.SDL_WINDOW_RESIZABLE
    sdl2.SDL_SetWindowFlags(sdl_window, new_flags)

class MyApp(App):
    def build(self):
        return Widget()

if __name__ == "__main__":
    MyApp().run()

注意事项

  • 该方案仅适配Kivy默认的SDL2后端,如果你使用其他自定义后端需要对应调整原生接口调用
  • 配置代码需要放在App实例的run()方法执行前,否则配置不会生效

内容的提问来源于stack exchange,提问作者deepan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 07:27:03