【发布时间】:2019-04-12 01:34:11
【问题描述】:
我是 kivy 的新手。 我创建了一个带有 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() 方法。
【问题讨论】:
-
尝试打印用户名和密码以外的其他内容。只是“测试”之类的
-
我尝试在每一行之后放置打印语句,但函数中没有任何内容打印出来
-
在
routine()中使用yield使其成为生成器函数。这是你的意图吗? -
我想我应该更清楚。当你调用一个生成器函数时,它并不执行函数代码,而是返回一个迭代器,当被调用时,它将执行原始函数代码。
-
当我从以前不工作的项目中复制该位时,我什至没有注意到 yield 语句。我删除了它,现在它可以工作了。
标签: python-3.x kivy kivy-language