Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Kivy-base应用程序具有奇怪的对齐方式_Python_Kivy - Fatal编程技术网

Python Kivy-base应用程序具有奇怪的对齐方式

Python Kivy-base应用程序具有奇怪的对齐方式,python,kivy,Python,Kivy,我正在尝试构建一个基本的Kivy应用程序。添加基本元素并运行应用程序后,所有元素都会塞进左下角。它在android和Linux上显示为这样 Main.py: from kivy.app import App from kivy.uix.widget import Widget class SublimeLauncher(Widget): pass class SublimeLauncherApp(App): def build(self): return Su

我正在尝试构建一个基本的Kivy应用程序。添加基本元素并运行应用程序后,所有元素都会塞进左下角。它在android和Linux上显示为这样

Main.py:

from kivy.app import App
from kivy.uix.widget import Widget

class SublimeLauncher(Widget):
    pass

class SublimeLauncherApp(App):
    def build(self):
        return SublimeLauncher()

if __name__ == "__main__":
    SublimeLauncherApp().run()
升华辐射计,千伏:

#:kivy 1.2.0
<SublimeLauncher>:
    FloatLayout:
        BoxLayout:
            orientation: 'vertical'
            spacing: 10
            Label:
                text: "Enter the path to the folder to open.\nPress OK if you would like to open without a directory"
            TextInput:
                id: folderpath
            Button:
                text: 'OK'
#:kivy 1.2.0
:
浮动布局:
盒子布局:
方向:“垂直”
间距:10
标签:
text:“输入要打开的文件夹的路径。\n如果要在没有目录的情况下打开,请按“确定”
文本输入:
id:folderpath
按钮:
文字:“OK”
我第一次只是用BoxLayout尝试了一下,但是在某个地方读到了根部件总是和应用程序一样大。如何声明应用程序的大小?还是布局?您将如何着手进行类似对话框的操作

也许我遗漏了一些非常基本的东西,但我似乎无法理解

编辑:这是我看到的


您的布局的默认大小为100x100像素。您可以尝试将其着色,以查看它占用多少空间:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

kv = '''
<SublimeLauncher>:
    BoxLayout:
        canvas:
            Color:
                rgb: 1, 0, 0
            Rectangle:
                size: self.size
        orientation: 'vertical'
        spacing: 10
        Label:
            text: "Enter the path to the folder to open.\\nPress OK if you would like to open without a directory"
        TextInput:
            id: folderpath
        Button:
            text: 'OK'
'''
Builder.load_string(kv)

class SublimeLauncher(Widget):
    pass

class SublimeLauncherApp(App):
    def build(self):
        return SublimeLauncher()

if __name__ == "__main__":
    SublimeLauncherApp().run()
从kivy.app导入应用
从kivy.uix.widget导入widget
从kivy.lang导入生成器
kv='''
:
盒子布局:
画布:
颜色:
rgb:1,0,0
矩形:
大小:self.size
方向:“垂直”
间距:10
标签:
text:“输入要打开的文件夹的路径。\\n如果要在没有目录的情况下打开,请按“确定”
文本输入:
id:folderpath
按钮:
文字:“OK”
'''
建筑商荷载(千伏)
类升华工具(小部件):
通过
类升华UncheraPP(应用程序):
def生成(自):
返回升华器()
如果名称=“\uuuuu main\uuuuuuuu”:
升华uncherapp().run()
设置非默认大小:

kv = '''
<SublimeLauncher>:
    BoxLayout:
        size: 250, 250
        canvas:
            Color:
                rgb: 1, 0, 0
            Rectangle:
                size: self.size
        orientation: 'vertical'
        spacing: 10
        Label:
            text: "Enter the path to the folder to open.\\nPress OK if you would like to open without a directory"
        TextInput:
            id: folderpath
        Button:
            text: 'OK'
'''
Builder.load_string(kv)
kv=''
:
盒子布局:
尺码:250250
画布:
颜色:
rgb:1,0,0
矩形:
大小:self.size
方向:“垂直”
间距:10
标签:
text:“输入要打开的文件夹的路径。\\n如果要在没有目录的情况下打开,请按“确定”
文本输入:
id:folderpath
按钮:
文字:“OK”
'''
建筑商荷载(千伏)
占用全部空间:

kv = '''
<SublimeLauncher>:
    BoxLayout:
        size: root.size
        canvas:
            Color:
                rgb: 1, 0, 0
            Rectangle:
                size: self.size
        orientation: 'vertical'
        spacing: 10
        Label:
            text: "Enter the path to the folder to open. \\nPress OK if you would like to open without a directory"
        TextInput:
            id: folderpath
        Button:
            text: 'OK'
'''
Builder.load_string(kv)
kv=''
:
盒子布局:
大小:root.size
画布:
颜色:
rgb:1,0,0
矩形:
大小:self.size
方向:“垂直”
间距:10
标签:
text:“输入要打开的文件夹的路径。\\n如果要在没有目录的情况下打开,请按“确定”
文本输入:
id:folderpath
按钮:
文字:“OK”
'''
建筑商荷载(千伏)

由于您的根小部件不是布局(您制作了
submielauncher
inherit
widget
),因此它不会设置其子部件的大小/位置。因此,您的
FloatLayout
具有默认值,因为您也不会手动覆盖它们

pos: 0, 0
size: 100, 100
当然,这些默认值会约束子级,因为
FloatLayout
会根据其size\u hint属性约束其大小

正如尼卡金指出的,你想给他们更多的空间

此外,由于文本比标签大(您也没有设置halign和text_size),因此它的纹理集中在标签的中心,因此它的某些部分在屏幕外。你想看看kivy/examples/widgets/textalign.py,我最近写了一篇关于编程接口时使用的小技巧的文章。该技巧将让您看到添加到屏幕上的所有小部件(包括布局)周围的边框。这将是代码的结果:

它利用继承和Kivy规则覆盖所有小部件的基类。您只需添加:

<Widget>:
    canvas.after:
        Line:
            rectangle: self.x+1,self.y+1,self.width-1,self.height-1
:
在下列情况之后:
行:
矩形:self.x+1,self.y+1,self.width-1,self.height-1
在该文件的开头:

<SublimeLauncher>:
    FloatLayout:
        BoxLayout:
            orientation: 'vertical'
            spacing: 10
            Label:
                text: "Enter the path to the folder to open.\nPress OK if you would like to open without a directory"
            TextInput:
                id: folderpath
            Button:
                text: 'OK'
:
浮动布局:
盒子布局:
方向:“垂直”
间距:10
标签:
text:“输入要打开的文件夹的路径。\n如果要在没有目录的情况下打开,请按“确定”
文本输入:
id:folderpath
按钮:
文字:“OK”

这两个都是很好的答案。我只是觉得另一个答案解释得更好一些。谢谢你的例子。