无法使用“ root.top-self.height”正确放置在奇异果中

时间:2018-11-24 10:35:39

标签: python 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()

1 个答案:

答案 0 :(得分:0)

消除BoxLayout中未使用的y并将size_hint的{​​{1}}组件设置为1.0意味着每个按钮将在{{1} }。

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__()是默认值,在实际显示该应用程序之前不会更新。