简体   繁体   中英

how can i change the text box position so it will be next to the button?(kivy)

i have a kivy code that makes a label, button and a text box. i want to put the textbox next to the button and not under it, how can i do that?

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()

You could create another BoxLayout with horizontal orientation to harbor the Button and 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

If you want to change the size of the Button and TextInput, you can set that in the secondary BoxLayout:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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