簡體   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