繁体   English   中英

如何在python中使用kivy在TabeedPanel中使用GridLayout

[英]how to use GridLayout in TabeedPanel using kivy in python

我正在尝试使用kivy和TabeedPanel在python中创建GUI。 放置标签,TextInput和按钮的确切位置时会遇到一些问题。 我无法将多个标签TextInput完全放在一起。 这就是为什么我在代码中评论。 我也尝试了GridLayout,但是无法准确安排。 你能帮助我吗? 提前致谢。

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelItem
from kivy.lang import Builder
from kivy.uix.checkbox import CheckBox
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.textinput import TextInput
import json

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'
        BoxLayout:
            Label:
                text: 'label'
            TextInput:
                text: 'TextInput'
            CheckBox: 
                text: 'CheckBox'
            Button:
                text: 'save'

    #BoxLayout:
     #   orientation: 'vertical'
      #  BoxLayout:
       #     orientation: 'horizontal'
        #    Label:
         #       text: 'label'



    TabbedPanelItem:
        text: 'page2'
        BoxLayout:
            Label:
                text: 'number1'
        #TextInput:
        #   text: 'TextInput'
            Label:
                text: 'number2'
       # TextInput:
       #    text: 'TextInput'
            Button:
                text: 'button'

""")

class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


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

基维贵

按照您的示例,可以使用BoxLayouts,但需要正确嵌套它们:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder

Builder.load_string("""

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'

        BoxLayout:
            padding: 50, 50, 50, 50
            orientation: 'horizontal'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 1
                Label:
                    text: 'label'
                Label:
                    text: 'label'
                Label:
                    text: 'label'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                TextInput:
                    text: 'TextInput'
                TextInput:
                    text: 'TextInput'
                TextInput:
                    text: 'TextInput'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 0.40
                CheckBox: 
                    text: 'CheckBox'
                CheckBox: 
                    text: 'CheckBox'
                CheckBox: 
                    text: 'CheckBox'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                size_hint_x: 0.60
                Button:
                    text: 'save'
                Button:
                    text: 'save'
                Button:
                    text: 'save'


    TabbedPanelItem:
        text: 'page2'

        BoxLayout:
            padding: 50, 50, 50, 50
            orientation: 'horizontal'

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                Label:
                    text: 'label'
                Label:
                    text: 'label'
                Label:

            BoxLayout:
                spacing: 50
                orientation: 'vertical'
                TextInput:
                    text: 'TextInput'
                TextInput:
                    text: 'TextInput'
                Button:
                    spacing: 100
                    text: 'button'

""")

class Test(TabbedPanel):
    pass

class MyApp(App):

    def build(self):
        test = Test()
        return test


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

输出:

在此处输入图片说明

这是一个使用GridLayout的示例,我在另一个问题中引用了该示例。 仅供参考,有很多方法可以解决此问题。 我个人喜欢将gridlayout与表单一起使用,因为如果需要,可以很容易地放置ScrollViews。

请在此处阅读kv语言以帮助保持事物干燥。 如果在kv中定义了窗口小部件,则无需将其导入文件的顶部。

例:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder

Builder.load_string("""
<MyLabel@Label>:
    size_hint: (None, None)
    size: (400, 100)

<MyTextInput@TextInput>:
    size_hint: (None, None)
    size: (600, 100)

<MyButton@Button>:
    size_hint: (None, None)
    size: (400, 100)

<MyCheckBox@AnchorLayout>:
    # I'm nesting the checkbox here b/c it is hard to see if the background is not lightened.
    size_hint: (None, None)
    size: (100, 100)
    anchor_x: "center"
    anchor_y: "center"
    canvas.before:
        Color:
            rgba: [0.7, 0.7, 0.7, 1]
        Rectangle:
            pos: self.pos
            size: self.size
    CheckBox:

<Test>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'page1'
        GridLayout:
            rows: 3
            cols: 4
            padding: [10, 100]
            spacing: [10, 50]
            MyLabel:
                text: "Label 1"
            MyTextInput:
            MyCheckBox:
            MyButton:
                text: "Button 1"
            MyLabel:
                text: "Label 3"
            MyTextInput:
            MyCheckBox:
            MyButton:
                text: "Button 2"
            MyLabel:
                text: "Label 3"
            MyTextInput:
            MyCheckBox:
            MyButton:
                text: "Button 3"


    TabbedPanelItem:
        text: 'page2'
        GridLayout:
            rows: 3
            cols: 2
            padding: [10, 100]
            spacing: [10, 50]
            MyLabel:
                text: "Label 1"
            MyTextInput:

            MyLabel:
                text: "Label 2"
            MyTextInput:

            # blank spacer widget
            Widget:
                size_hint: (None, None)
                size: (400, 100)
            MyButton:
                text: "Button"
""")


class Test(TabbedPanel):
    pass


class MyApp(App):

    def build(self):
        return Test()


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM