2017-07-17 21 views
0

私はkivyとTabeedPanelを使って、Pythonで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を使うのが好きです。なぜなら、必要に応じてScrollViewを置くのは簡単だからです。

乾燥物などの物を守るために、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を動的に埋める方法は?これらの値はマップ(キー、値のペア)として変数に格納され、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

関連する問題