Kivy Window.size不会更改窗口的大小

时间:2019-04-15 17:34:17

标签: python python-3.x kivy

我正在用kivy创建一个应用程序,它应该从一个窗口大小转到另一个窗口大小。但是,当我调用Window.size =()时,它实际上并没有更改窗口的大小。

我尝试将Window.size设置在几个不同的位置,

main.py

from kivy.config import Config
#Config.set('graphics', 'resizable', False)

from kivy.app import App

from kivy.properties import StringProperty, ObjectProperty

from kivy.core.window import Window

from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition, NoTransition

from kivy.clock import Clock

import csv
import paramiko
import time
#import os

def load_csv(filepath):
    with open(filepath, newline='') as csvfile:
        file_array = list(csv.reader(csvfile))
        csvfile.close() 
    return file_array

class Client(Screen):

    Window.size = (1280, 720)

    def client(self, ssh, sftp):

        print("test")

class Connect(Screen):
    Window.size = (600, 300)

    def routine(self, host, port, username, password):

        ssh = None
        sftp = None

        #print(username, password)
        self.ids.status.text = "connecting"

        try:
            self.ids.status.text = "attempting to connect to " + host + ":" + str(port)
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(host, port, username, password)

            transport = paramiko.Transport((host, port))
            transport.connect(username = username, password = password)
            sftp = paramiko.SFTPClient.from_transport(transport)

            self.ids.status.text = "connected to " + host + ":" + str(port)

            Clock.schedule_once(self.continue_to_client, 2)
            self.manager.get_screen('client').client(ssh, sftp)


        except Exception as e:
            if sftp is not None:
                sftp.close()
            if ssh is not None:
                ssh.close()

            self.ids.status.text = "connection failed: " + str(e)
            Clock.schedule_once(self.return_to_login, 2)
            #self.manager.current = 'login'

    def return_to_login(self, *args):
        self.manager.transition = SlideTransition(direction = "right")
        self.manager.current = 'login'
            #time.sleep(5)

    def continue_to_client(self, *args):

        self.manager.transition = NoTransition()
        self.manager.current = 'client'


class Login(Screen):
    Window.size = (600, 300)
    def do_login(self, loginText, passwordText, hostText, portText):
        app = App.get_running_app()

        if hostText == "":
            hostText = "slurm.imsa.edu"
        if portText == "":
            portText = "22"

        host = hostText
        port = int(portText)

        username = loginText
        password = passwordText

        self.manager.transition = SlideTransition(direction = "left")
        self.manager.current = "connect"

        self.manager.get_screen('connect').routine(host, port, username, password)

    def resetForm(self):
        self.ids['login'].text = ""
        self.ids['password'].text = ""

target_x = 600
target_y = 300

manager = ScreenManager()

class BrummetApp(App):

    username = StringProperty(None)
    password = StringProperty(None)

    screenName = StringProperty(None)

    title = 'Brummet Client v ' + load_csv("data/meta")[0][1]

    def check_resize(self, instance, x, y):
        # resize X
        #screenName = manager.current
        #print(screenName)
        if manager.current != "client":

            if x >  target_x:
                Window.size = (target_x, Window.size[1])

            if y > target_y:
                Window.size = (Window.size[0], target_y)

            if x <  target_x:
                Window.size = (target_x, Window.size[1])

            if y < target_y:
                Window.size = (Window.size[0], target_y)

    def build(self):

        manager.add_widget(Login(name = 'login'))
        manager.add_widget(Connect(name = 'connect'))
        manager.add_widget(Client(name = 'client'))

        Window.bind(on_resize=self.check_resize)

        return manager

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

brummet.kv

<Login>:
    BoxLayout
        id: login_layout
        orientation: 'vertical'
        padding: [10,10,10,10]
        spacing: 10

        BoxLayout:
            spacing: 10
            orientation:'vertical'

            Label:
                id: title
                text: 'Brummet Client'
                halign: 'center'
                valign: 'middle'
                font_size: 24

            Label:
                text: 'Please log in with IMSA SLURM credentials'
                halign: 'center'
                valign: 'middle'
                font_size: 20

        BoxLayout:
            orientation: 'horizontal'
            Label:
                size_hint: (0.15, 1)
                text: 'Username'
                font_size: 18
                halign: 'left'

            TextInput:
                size_hint: (0.7, 1)
                id: username
                multiline: False
                font_size: 18
                write_tab: False

        BoxLayout:
            orientation: 'horizontal'
            Label:
                size_hint: (0.15, 1)
                text: 'Password'
                halign: 'left'
                font_size: 18

            TextInput:
                size_hint: (0.7, 1)
                id: password
                multiline: False
                password: True
                font_size: 18
                write_tab: False

        BoxLayout:
            orientation: 'horizontal'
            Label:
                size_hint: (0.15, 1)
                text: 'Host'
                halign: 'left'
                font_size: 18

            TextInput:
                size_hint: (0.7, 1)
                hint_text: 'slurm.imsa.edu'
                id: host
                multiline: False
                font_size: 18
                write_tab: False

        BoxLayout:
            orientation: 'horizontal'
            Label:
                size_hint: (0.15, 1)
                text: 'Port'
                halign: 'left'
                font_size: 18

            TextInput:
                size_hint: (0.7, 1)
                input_type: 'number'
                input_filter: 'int'
                hint_text: '22'
                id: port
                multiline: False
                font_size: 18
                write_tab: False

        Button:
            text: 'Log In'
            font_size: 24
            id: submit
            on_press:
                root.do_login(username.text, password.text, host.text, port.text)

<Connect>:
    BoxLayout:
        orientation: 'vertical'
        padding: [0,100,0,100]
        spacing: 0

        Label:
            text:'Logging In'
            font_size: 24
            halign: 'center'
            valign: 'middle'

        Label:
            id: status
            test:''
            font_size: 12
            halign: 'center'
            valign: 'middle'
            text_size: self.size
            size_hint: 1,1
            shorten: True

<Client>:
    BoxLayout:
        orientation: 'horizontal'
        padding: [5, 5, 5, 5]
        spacing: 0
        Button:
        Button:

输入Client()时,代码应将窗口大小更改为1280 x 720,但是,以前的600 x 300窗口仍然存在。

1 个答案:

答案 0 :(得分:0)

要更改每个屏幕的大小,请使用ScreenManager on_pre_enter事件。

摘要

class Client(Screen):

    def on_pre_enter(self, *args):
        Window.size = (1280, 720)