如何更改文本框位置,使其位于按钮旁边?(kivy)

时间:2017-03-17 07:11:45

标签: python button text position kivy

我有一个kivy代码,可以制作标签,按钮和文本框。我想把文本框放在按钮旁边而不是在它下面,我该怎么做?

import socket
import sys
import os
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.bubble import Bubble
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
class TextInputApp(App):
    def build(self):

        layout = BoxLayout(padding=10, orientation='vertical')
        btn1 = Button(text="OK", size_hint=(0.49, 0.1),pos_hint={'x': .51, 'center_y': .5})
        btn1.bind(on_press=self.buttonClicked)
        layout.add_widget(btn1)
        self.txt1 = TextInput(multiline=False, text='',
                                           size_hint=(0.5, 0.1))
        layout.add_widget(self.txt1)
        self.lbl1 = Label(text="Write your guess in the blank text box", size_hint=(1, None), height=30)
        layout.add_widget(self.lbl1)

        return layout

    def buttonClicked(self,btn):
        print "hi"

TextInputApp().run()

1 个答案:

答案 0 :(得分:0)

您可以创建另一个水平方向的BoxLayout来包含Button和TextInput。

class TextInputApp(App):
    def build(self):

        layout = BoxLayout(padding=10, orientation='vertical')
        # Second boxlayout
        layout2 = BoxLayout()
        # Add BoxLayout do main layout
        layout.add_widget(layout2)

        # Drop old size and pos_hints
        btn1 = Button(text="OK")
        btn1.bind(on_press=self.buttonClicked)
        # Add Button to secondary boxlayout
        layout2.add_widget(btn1)
        self.txt1 = TextInput(multiline=False, text='',
                                           size_hint=(0.5, 0.1))
        layout.add_widget(self.txt1)
        # Drop size_hint
        self.lbl1 = Label(text="Write your guess in the blank text box")
        layout2.add_widget(self.lbl1)

        return layout

如果要更改Button和TextInput的大小,可以在辅助BoxLayout中设置:

layout2 = BoxLayout(size_hint_y=None, height=30)