2017-07-17 104 views
0

我想在python中使用kivy和TabeedPanel制作GUI。一些问题将出现在标签的确切位置TextInput按钮上。我无法将多个标签,TextInput放在一起。这就是我在代码中评论的原因。我也试过GridLayout,但无法准确安排。 你能帮我吗?提前致谢。如何在Python中使用kivy在TabeedPanel中使用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() 

kivy gui

回答

2

下面是一个使用的例子GridLayout tha我在你的另一个问题中提到了一个参考。仅供参考,有很多方法可以解决这个问题。我个人喜欢在表单中使用gridlayout,因为如果需要的话,可以很容易地放置ScrollViews。

阅读kv语言here以帮助保持干燥和其他事物。如果一个小部件是用kv定义的,那么你不需要将它们导入到文件的顶部。

例子:

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

Builder.load_string(""" 
<[email protected]>: 
    size_hint: (None, None) 
    size: (400, 100) 

<[email protected]>: 
    size_hint: (None, None) 
    size: (600, 100) 

<[email protected]>: 
    size_hint: (None, None) 
    size: (400, 100) 

<[email protected]>: 
    # 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() 
+1

看起来不错。谢谢@Mox – crazyDelight

+1

如何动态填充标签和TextInput?像这些值存储在一个变量中作为一个映射(键,值对),并以这样的json格式var ='{“a”:“Goc”,“b”:“Coc”,“c”:“Dow” }')在运行时,使用任何循环,标签和TextInput应该自动填充。 N.B:这里的'关键'数据应该在标签处显示,'值'在TextInput处。即:a:Goc,b:Coc,c:Dow。 SO a,b,c是标签,Goc,Coc,Dow是TextInput。 – crazyDelight

+1

上面的东西只适用于“page1” – crazyDelight

2

按照你的例子,你可以使用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() 

输出:

enter image description here

+1

代码的工作非常出色。谢谢@FJSevilla – crazyDelight