Class 方法在 Kivy 中调用时不会 运行
Class method does not run when called upon in Kivy
我是kivy的新手。
我创建了一个包含 2 个文本字段的登录页面。我现在正尝试将变量传递到下一页,它将使用 python 的 ssh 客户端连接到服务器。但是,当我 运行 程序时,似乎我在第二个屏幕中调用的方法甚至没有 运行,因为调试输出显示 none。
我尝试了几种将变量传递给不同 class 的函数的方法,暂时我决定使用全局变量。我确信有更简单或更好的方法,但我无法首先获得 运行 的功能。
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 class 没问题,但是我无法 运行 .routine() 方法 on_enter。
yield ssh
正在阻止 Connect.routine()
执行。尝试将其注释掉。
我是kivy的新手。 我创建了一个包含 2 个文本字段的登录页面。我现在正尝试将变量传递到下一页,它将使用 python 的 ssh 客户端连接到服务器。但是,当我 运行 程序时,似乎我在第二个屏幕中调用的方法甚至没有 运行,因为调试输出显示 none。
我尝试了几种将变量传递给不同 class 的函数的方法,暂时我决定使用全局变量。我确信有更简单或更好的方法,但我无法首先获得 运行 的功能。
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 class 没问题,但是我无法 运行 .routine() 方法 on_enter。
yield ssh
正在阻止 Connect.routine()
执行。尝试将其注释掉。