首页 > 解决方案 > 无法使用“root.top-self.height”在 kivy 中正确放置

问题描述

我正在尝试使用 kivy 中的框布局创建菜单。我想使用“root.top-self.height”,以便它从屏幕顶部粘贴垂直布局,但仍然从底部粘贴。此外,当我打印(root.top)时,它奇怪地给出了 100,这不是我的屏幕分辨率。请让我知道如何准确放置它。此外,我在某处读到需要使用 root=BoxLayout(),现在在使用此按钮后,添加该按钮后不可点击,在添加此按钮之前,我可以使用按钮。请让我知道如何处理“root”,即屏幕或应用程序大小功能。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.label import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.layout import Layout
from  kivy.uix.button import Button
from kivy.lang import Builder



## CREATING A CLASS WHICH HAS SCREEN CONTENT:
class firstScreen(BoxLayout):
    def __init__(self,**kwargs):
        super(firstScreen, self).__init__(**kwargs)
        self.orientation = 'vertical'
        root = BoxLayout()
        self.pos = (0 ,root.top-self.height)
        print(root.top)

        self.myButton1 = Button(text='Home',
                              color = (1,0,0,1),
                              size_hint = (0.1,None),
##                              pos_hint = {'x':.8, 'y':'.7'},
##                              pos_hint = {'x':0, 'top':'0'},
                              pos = (0,0)
                              )
        self.myButton2 = Button(text='Buy Now',
                                color = (1,0,0,1),
                              size_hint = (0.1,None))
        self.myButton3 = Button(text='Blog',
                                color = (1,0,0,1),
                              size_hint = (0.1,None))
        self.myButton4 = Button(text='Contant Us',
                                color = (1,0,0,1),
                              size_hint = (0.1,None))

        self.add_widget(self.myButton1)
        self.add_widget(self.myButton2)
        self.add_widget(self.myButton3)
        self.add_widget(self.myButton4)

    def on_touch_down(self,touch):
        print(touch)
    def on_touch_move(self,touch):
        print(touch)
    def on_touch_up(self,touch):
        print(touch)

## CREATING A CLASS WHICH RETURNS SOME SCREEN:
class myKivyApp(App):
    def build(self):
        return firstScreen()

## THIS CODE RUNS THE CLASS WHICH HAS SOME SCREEN
if __name__ == "__main__":
    myKivyApp().run()

标签: pythonkivy

解决方案


消除未使用BoxLayout的并将y组件设置为size_hintto1.0意味着每个按钮将平等地共享可用于 的垂直空间firstScreen

class firstScreen(BoxLayout):
    def __init__(self,**kwargs):
        super(firstScreen, self).__init__(**kwargs)
        self.orientation = 'vertical'
        self.myButton1 = Button(text='Home',
                              color = (1,0,0,1),
                              size_hint = (0.1,1.0))
        self.myButton2 = Button(text='Buy Now',
                                color = (1,0,0,1),
                              size_hint = (0.1,1.0))
        self.myButton3 = Button(text='Blog',
                                color = (1,0,0,1),
                              size_hint = (0.1,1.0))
        self.myButton4 = Button(text='Contant Us',
                                color = (1,0,0,1),
                              size_hint = (0.1,1.0))

        self.add_widget(self.myButton1)
        self.add_widget(self.myButton2)
        self.add_widget(self.myButton3)
        self.add_widget(self.myButton4)

顺便说一句,root.top将永远100__init__()方法中。100是默认值,在应用实际显示之前不会更新。


推荐阅读