如何在中间放置一个kivy按钮? X 坐标不起作用

how to put a kivy button in the middle? X coordinates not working

我已经学了一半的 kivy 并且我已经停止使用它有一段时间了我忘记了一点所以无论如何我做了一个名为“Tally Counter”的小项目并且我想在中间和下方添加一个按钮 - 我已经完成了,突然 x 坐标不起作用 - 这是输出 -

代码-

from kivy.core import window
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import builder
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
import os, sys
from kivy.resources import resource_add_path, resource_find

builder.Builder.load_file("tally.kv")
Window.size = (450,600)
class Tally(Widget):
    pass

        

class Count(App):
    def build(self):
        return Tally()
if __name__ == "__main__":
    Count().run() 

最后但同样重要的是 KV 文件

<Tally>
    BoxLayout:
        orientartion:"vertical"
        size: root.width,root.height
        Button:
            text:"Count"
            size_hint: (None, None)
            height: 70
            width: 400
            pos_hint: {"center_x":.5,"center_y":.3}

但是我为忘记了kivy而感到内疚,我问这些问题有点愚蠢,以后我会问更好的问题。我会努力的。

参考这三个按钮希望你能从中找到你的解决方案

 # import modules
    import kivy
     
    # base Class of your App inherits from the App class.
    # app:always refers to the instance of your application
    from kivy.app import App
     
    # creates the button in kivy
    # if not imported shows the error
    from kivy.uix.button import Button
     
    # This layout allows you to set relative coordinates for children.
    from kivy.uix.relativelayout import RelativeLayout
     
    # To change the kivy default settings
    # we use this module config
    from kivy.config import Config
         
    # 0 being off 1 being on as in true / false
    # you can use 0 or 1 && True or False
    Config.set('graphics', 'resizable', True)
     
    # creating the App class
    class Pos_Size_App(App):
         
        def build(self):
     
            # A Relative Layout with a size of (300, 300) is created
            rl = RelativeLayout(size =(300, 300))
             
            # creating button
            # size of button is 20 % by height and width of layout
            # position is 'center_x':.7, 'center_y':.5
            b1 = Button(size_hint =(.2, .2),
                        pos_hint ={'center_x':.7, 'center_y':.5},
                        text ="pos_hint")
     
            # creating button
            # size of button is 20 % by height and 50 % width of layout
            b2 = Button(size_hint =(.5, .2), 
                        text ="size_hint")
     
                    # creating button
            # size of button is 20 % by height and width of layout
            # position is 200, 200 from bottom left
            b3 = Button( size_hint =(.2, .2),
                        pos =(200, 200),
                        text ="pos")
             
             
     
            # adding button to widget
            rl.add_widget(b1)
            rl.add_widget(b2)
            rl.add_widget(b3)
         
             
            # returning widget
            return rl
     
    # run the App
    if __name__ == "__main__":
        Pos_Size_App().run()

来自BoxLayout Documentation

Position hints are partially working, depending on the orientation:

If the orientation is vertical: x, right and center_x will be used.

If the orientation is horizontal: y, top and center_y will be used.

但是,您的 kv 文件中有错字。尝试更改:

orientartion:"vertical"

至:

orientation:"vertical"