在Kivy中调用时,类方法无法运行

时间:2019-04-12 01:34:12

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

我是新手。 我创建了一个包含2个文本字段的登录页面。我现在正尝试将变量传递到下一页,该页面将使用ssh客户端进行python连接到服务器。但是,当我运行该程序时,似乎我在第二个屏幕中调用的方法甚至没有运行,因为没有任何调试输出显示出来。

我尝试了几种将变量传递到不同类的函数中的方法,并且暂时开始使用全局变量。我敢肯定有一种更简便或更好的方法,但是我无法让该函数首先运行。

main.py

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

from kivy.app import App

from kivy.properties import StringProperty

from kivy.core.window import Window

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

import csv
import paramiko
#import os

global username
global password

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

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

        host = 'titanrobotics.ddns.net'
        port = 60022
        print(username, password)
        self.ids.status.text = "connecting"

        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        try:
            self.ids.status.text = "attempting to connect to " + host
            ssh.connect(host, port, username, password)
            yield ssh
            self.ids.status.text = "connected to " + host

        finally:
            ssh.close()
            self.ids.status.text = "connection failed"

        #print("here")
    #ssh = loginroutine(username, password)

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

        username = loginText
        password = passwordText

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

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

class BrummetApp(App):
    username = StringProperty(None)
    password = StringProperty(None)

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

    def build(self):
        manager = ScreenManager()

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

        return manager

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

brummet.kv

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

        BoxLayout:
            orientation:'vertical'
            padding: [0,0,0,0]
            spacing: 0

            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'
                spacing: -20
                font_size: 24

        BoxLayout:
            orientation: 'vertical'

            Label:
                text: 'Username'
                font_size: 18
                halign: 'left'
                text_size: root.width-20, 0

            TextInput:
                id: username
                multiline: False
                font_size: 16
                write_tab: False

        BoxLayout:
            orientation: 'vertical'
            Label:
                text: 'Password'
                halign: 'left'
                font_size: 18
                text_size: root.width-20, 0

            TextInput:
                id: password
                multiline: False
                password: True
                font_size: 16
                write_tab: False

        Button:
            text: 'Log In'
            font_size: 24

            on_press:
                root.do_login(username.text, password.text)

<Connect>:
    on_enter:
        root.routine()
    BoxLayout:
        orientation: 'vertical'
        padding: [0,125,0,125]
        spacing: 0

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

        Label:
            id: status
            test:''
            font_size: 16
            halign: 'center'
            valign: 'middle'

加载Connect类似乎很好,但是我无法在on_enter上运行.routine()方法。

1 个答案:

答案 0 :(得分:0)

yield ssh正在阻止Connect.routine()执行。尝试将其注释掉。