【问题标题】:Python: Kivy: How to align a label itself and text box?Python:Kivy:如何对齐标签本身和文本框?
【发布时间】:2019-12-06 21:50:08
【问题描述】:

我从这里尝试了多段代码,但它们似乎都将标签或文本框中的文本对齐。我想垂直和水平对齐文本框和标签。我是kivy的初学者,如果答案很明显,请见谅。

  1. 有没有办法对齐标签本身而不是里面的文本?
  2. 还有一种方法可以对齐文本框本身而不是里面的文本吗?

提前致谢!

这是我的 .kv 文件 firstkivy.kv

#Filename: firskivy.kv

<MyGrid>:
    Label:
        text: "Writing"
        font_size: 80
        bold: True
        color: 0.204, 0.204, 0.204, 1
        size: self.texture_size
        text_size: self.size
        halign: 'center'
        valign:'middle'
        canvas.before:
            Color:
                rgba: 0.549, 1, 0.984, 1
            Rectangle:
                size: self.size
                pos: self.pos


    TextInput:
        size_hint: (.2, None)
        height: 100
        width: 360
        multiline: True
        size_hint: (None, None)
        font_size: 30
        halign: 'right'
        pos_hint: (None, None)
        focus: True

这是 .py 文件 firstkivy.py

# setting up and fixing the window size
#to prevent it from being resized
from kivy.config import Config
Config.set('graphics', 'resizable', False)
Config.set('graphics','width', '360')
Config.set('graphics', 'height', '540')

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window

# going back to this one...
Window.clearcolor = (0.549, 1, 0.984, 1)

class MyApp(Widget):
    pass

#building the app
class FirstKivy(App, BoxLayout):
    def build(self):

        self.title = 'Arabic Language Learning App'
        return MyApp()

# running the app
if __name__ == '__main__':
    FirstKivy().run()

这里是 Output

【问题讨论】:

标签: python alignment kivy kivy-language


【解决方案1】:

试着像这样写你的firstkivy.py

from kivy.config import Config
Config.set('graphics', 'resizable', False)
Config.set('graphics','width', '360')
Config.set('graphics', 'height', '540')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window


class MyApp(BoxLayout):
    pass


class FirstKivy(App):
    title = 'Arabic Language Learning App'
    def build(self):

        return MyApp()


if __name__ == '__main__':
    FirstKivy().run

还有你的firstkivy.kv 喜欢:

<MyApp>:
    orientation: "vertical"
    spacing: 10
    space_x: self.size[0]/3
    canvas.before:
        Color:
            rgba: 0.549, 1, 0.984, 1
        Rectangle:
            size: self.size
            pos: self.pos

    FloatLayout:

        Label:
            text: "Writing"
            font_size: 80
            bold: True
            color: 0.204, 0.204, 0.204, 1
            pos_hint: {'center_x': .5, 'center_y': .8}
            #size: self.texture_size
            #text_size: self.size
            #halign: 'center'
            #valign:'middle'


        TextInput:
            size_hint: 1, .5
            #height: 100
            #width: 200
            multiline: True
            pos_hint: {'center_x': .5}
            #size_hint: None, None
            #font_size: 30
            #halign: 'right'
            #pos_hint: None, None
            #focus: True

希望它能达到你想要的。

【讨论】:

  • pos_hint 中使用center_x 将对齐Widget 中心。您可以使用x 对齐Widgets 左边缘,或使用right 对齐Widgets 右边缘。
猜你喜欢
  • 2010-11-13
  • 2017-04-08
  • 2010-12-22
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 2018-10-13
相关资源
最近更新 更多