当窗口调整大小时,kivy按钮不完美

时间:2017-06-19 13:15:54

标签: python-3.x kivy kivy-language

我创建了一个带有GridLayout的ScrollView,它有一堆按钮。我的问题是,当改变窗口大小时,我无法使按钮成为完美的正方形。

.py文件:

from kivy.app import App

from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.scrollview import ScrollView

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout


class LogInScreen(Screen):
    pass
class EmployeeScreen(Screen):
    pass
class MyLayout(GridLayout):
    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.size_hint_y = (None)
        self.bind(minimum_height = self.setter('height'))



class Manager(ScreenManager):
    login_screen = ObjectProperty(None)
    employee_screen = ObjectProperty(None)


class CptApp(App):
    icon = 'Images\login\cptlogo.png'
    title = 'CPT'
    def build(self):
        return Manager()

if __name__=='__main__':
    CptApp().run()
enter code here

.kv文件:

#: import Window kivy.core.window.Window
<Manager>:
id: screen_manager

login_screen: login_screen
employee_screen: employee_screen


LogInScreen:
    id: login_screen
    name: 'login'
    manager: screen_manager

    FloatLayout:
        StackLayout:
            orientation: 'lr-tb'
            canvas:
                Color:
                    rgba: 1,1,1,1
                Rectangle:
                    pos: self.pos
                    size: self.size
            Image:
                size_hint_y: .1
                source: 'Images\login\cptbanner.jpg'
                allow_stretch: True
                keep_ratio: True

            Image: 
                source: 'Images\login\HD7Brw.jpg'
                allow_stretch: True
                keep_ratio: False

    Label:
        size_hint_y: .05
        size_hint_x: .5
        pos_hint: {"x": .25, "y": .7}
        markup: True
        text: '[i][b][color=#000000]USER NAME[/color][/b][/i]'

    TextInput:
        id: 'username_input'
        multiline: False
        size_hint_x: .4
        size_hint_y: .05
        pos_hint: {"x": .3, "y": .65}

    Label:
        size_hint_y: .05
        size_hint_x: .5
        markup: True
        text: '[i][b][color=#000000]PASSWORD[/color][/b][/i]'
        pos_hint: {'x': .25, 'y': .5}

    TextInput:
        id: 'password_input'
        multiline: False
        password: True
        size_hint_x: .4
        size_hint_y: .05
        pos_hint: {'x': .3, 'y': .45}

    Image:
        source: 'Images/login/loginbutton.png'
        size_hint_x: .25
        size_hint_y: .1
        pos_hint: {'x': .375, 'y': .25}

    Button:
        id: 'login_button'
        background_color: 0,0,0,0
        markup: True
        text: '[i][b][color=#000000]LOGIN[/color][/b][/i]'
        size_hint_x: .25
        size_hint_y: .1
        pos_hint: {'x': .375, 'y': .25} 
        on_release: screen_manager.current = 'employeescreen'

EmployeeScreen:
    id: employee_screen
    name: 'employeescreen'
    manager: screen_manager

    StackLayout:
        orientation: 'lr-tb'
        canvas:
            Color:
                rgba: 1,1,1,1
            Rectangle:
                pos: self.pos
                size: self.size
        Image:
            size_hint_y: .1
            source: 'Images\login\cptbanner.jpg'
            allow_stretch: True
            keep_ratio: True

        ScrollView:
            size: (Window.width, Window.height)

            MyLayout:
                cols: 2                 
                height: self.minimum_height
                pos: root.pos

                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'    
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'
                Button:
                    height: 40
                    size_hint_y: None
                    text: 'TEST'                    

enter code here

我希望按钮的高度与宽度无关。

1 个答案:

答案 0 :(得分:2)

您可以简单地将宽度设置为相同的常量,如下所示:

Button:
   height: 40
   size_hint_y: None
   width: 40
   size_hint_x: None
   text: 'TEST'

这会产生完美的正方形,但我想这不是你想要的。

this

另一个非常简单的事情是将按钮的高度设置为与其宽度相同(字面意思):

Button:
    size_hint_y: None
    height: self.width
    text: 'TEST'

产生了不错的结果。

result

并且,当调整大小时:

resized

完美的方块仍在。