【问题标题】:How can I center my GridLayout in the middle of the screen in Kivy?如何在 Kivy 中将我的 GridLayout 置于屏幕中间?
【发布时间】:2023-02-04 05:51:43
【问题描述】:

我有一个带有 8 列的 GridLayout,我添加了 64 个按钮。 (所以 8x8)。 我希望按钮始终是二次方的,所以我在我的 spot_init() 函数中做了它。

这一切都很好。当我使窗口变小或变大时,屏幕的其余部分变黑并且 GridLayout 留在角落。但我希望它居中。

对于 leftright 来说效果很好,但是当我尝试将其应用于 updown 时,它做了一些奇怪的事情,我真的无法解释。

我(也许)发现了一些事情:

  • 当我完全按照现在的方式进行操作时,但在代码中,由于某种原因,Y 坐标是应有值的 3 倍。
  • 当我将它除以 3 时,它会变成原来的 7 倍...
  • 如果我在 .kv 或 .py 文件中执行它不会改变
  • 在没有 RelativeLayout 的情况下移动 GridLayout 也不起作用(几乎发生同样的事情)
  • 其他提问者似乎有同样的问题(或类似的问题),但他们的修复对我没有帮助。

我的 .kv 文件:

RMainBoard:

<MainBoard>:
    cols:8
    # height: self.minimum_height
    # size_hint_y: None
    # size_hint_x: None

<RMainBoard@RelativeLayout>:
    pos:(self.width/2-min(self.width/8,self.height/8)*4,self.height/2-(min(self.width/8,self.height/8))*4)
    MainBoard:

我的 .py 文件:

#resize window (NEEDS TO BE AT VERY TOP)
from kivy.config import Config
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '600')

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.properties import NumericProperty

class MainBoard(GridLayout):
    spots = []
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.spot_init()
        
    def on_size(self,*args):
        for spot in self.spots:
            spot_size = min(self.width/8,self.height/8)
            print(min(self.width/8,self.height/8))
            spot.height = spot_size
            spot.width = spot_size

    def spot_init(self):
        for i in range(0,64):
            self.spots.append(Button(size_hint=(None,None),height=self.height/8,width=self.width/8))
            self.add_widget(self.spots[i])

class TestApp(App):
    pass
TestApp().run()

非常感谢 <3

【问题讨论】:

  • “我希望按钮始终是二次方的”是什么意思?你想要固定尺寸Buttons吗?它们必须总是方形的吗?
  • 尺寸不固定,但按钮的长度应与高度相等。 (所以你可以说“固定比例,但不是固定大小”

标签: python windows kivy kivy-language grid-layout


【解决方案1】:

与其编写自己的代码来定位 GridLayout,不如使用 kv 语言来实现。这是执行此操作的代码的修改版本:

# resize window (NEEDS TO BE AT VERY TOP)
from kivy.config import Config
from kivy.lang import Builder

Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '600')

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

kv = '''
RMainBoard:

<RMainBoard@RelativeLayout>:
    MainBoard:
        cols: 8
        size_hint: None, None
        
        # make MainBoard square
        width: min(root.width, root.height)
        height: self.width
        
        # position MainBoard in center of RMainBoard
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    
<SquareButton>:
    size_hint: None, None
    
    # make the Button square
    width: min(self.parent.width/8, self.parent.height/8) if self.parent else 100
    height: self.width
'''


class SquareButton(Button):
    pass


class MainBoard(GridLayout):
    spots = []

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.spot_init()

    def spot_init(self):
        for i in range(0, 64):
            self.spots.append(SquareButton(text=str(i)))
            self.add_widget(self.spots[i])


class TestApp(App):
    def build(self):
        return Builder.load_string(kv)


TestApp().run()

请注意,我将 kv 作为字符串包含在 python 代码中。那只是为了我自己的方便,它也可以在您的kv 文件中。

我在 kv 中定义了一个 SquareButton 类和一个 &lt;SquareButton&gt; 规则,它保持 SquareButton 方块。在spot_init()方法中创建的Buttons现在是SquareButtons

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多