您好,我目前正在Windows上使用kivy创建GUI。我正在使用它来ssh到Raspberry Pi,拍摄图像,然后将其scp回到我的Windows机器上。我已经使用GUI成功完成了这项工作。我有两个屏幕。首先是登录。一旦我登录第二个屏幕出现按钮和图像。登录屏幕和按钮正常运行其功能。但是,图像文件不会像我想要的那样更新。要么我希望它在某个时间间隔内更新,要么在我拍照后更新。
这是 Second Screen Interface。我想让右上方的图片自动更新。 "拍照"按钮拍照然后将其发送到我的计算机,我想在GUI中刷新图像并显示它
我的主要python文件,我打电话给#34; ScreenChange2.py"如下所示
import kivy
import os
kivy.require('1.10.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.uix.widget import Widget
from sshtest import ssh #import the ssh class from the ssh python file
class ScreenOne(Screen):
def login(self): #define login using ssh as a function
#Make a Class Variable called connection. This allows for other
#classes to call on it without passing it as an argument
ScreenOne.connection = ssh("192.168.1.3", "pi", "seniordesign")
#ScreenOne.connection.sendCommand("ls")
#ScreenOne.connection.sendCommand("mkdir thisistest")
print("Logging in") #For error checking
def gpioSet(self): #allows for gpio pins to trigger image capture
ScreenOne.connection.sendCommand("echo '18' > /sys/class/gpio/export")
ScreenOne.connection.sendCommand("echo 'out' > /sys/class/gpio/gpio18/direction")
class ScreenTwo(Screen): #This is where all the functions are
def command(self, input): #create a function that sends command through ssh
ScreenOne.connection.sendCommand(input) #Call on connection made before to send command
class MyScreenManager(ScreenManager):
pass
#Create the Application that will change screens. Add App(App) to end of wanted classname
class ScreenChangeApp(App):
#Create a function that builds the app. Keyword self make sures to reference
#current instantiation
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name = "screen_one"))
screen_manager.add_widget(ScreenTwo(name = "screen_two"))
return screen_manager #after building the screens, return them
#MyScreenManager.login()
sample_app = ScreenChangeApp()
sample_app.run()
KV文件如图所示
#: import os os
<CustomButton@Button>:
font_size: 12
size_hint: .2,.1
<Picture@Image>:
id: image
<ScreenOne>: #define The First Screen
BoxLayout: #Use box layout
Button: #create button
text: "Connect to the Raspberry Pi"
on_press:
root.login()
root.gpioSet()
root.manager.transition.direction= "left"
root.manager.transition.duration = 1
root.manager.current = "screen_two"
<ScreenTwo>:
BoxLayout:
spacing: 10
padding: 10
orientation: "vertical"
CustomButton:
text: "Send an ls command"
on_press:
root.command("ls")
CustomButton:
text: "Take picture"
on_press:
root.command("python cameradaemon.py &") #'&' runs script in background
root.command("sleep .1")
root.command("echo '1' > /sys/class/gpio/gpio18/value") #set pin high to take pic
root.command("echo '0' > /sys/class/gpio/gpio18/value") #take it off to prevent another photo
root.command("scp TEST.jpg Jason@192.168.1.10:C:/Users/Jason/Desktop/RaspberryPiTransferred")
root.command("kill %1")
CustomButton:
text: "Create Histogram"
on_press:
os.system("cd C:/Users/Jason/Desktop/KivyFiles/Histogram & python histogram.py")
AnchorLayout:
anchor_x: 'right'
anchor_y: 'top'
padding: 10
BoxLayout
size_hint: .3, .3
Image:
source: 'C:/Users/Jason/Desktop/RaspberryPiTransferred/TEST.jpg'
正如您可能知道的那样,在KV文件的末尾,我将图像文件作为静态插入到框布局中。我理解这就是为什么我的图片不会更新,但我需要知道的是如何自动更新它。
我以为我可以制作一个自定义图片规则,我从顶部开始
<Picture@Image>:
我也理解有一个图像文件的Reload()函数,但我不明白如何在KV文件中实现它。
我尝试在主文件中创建一个每秒运行重新加载功能的类,但图像没有显示,因为它没有链接到KV文件中的任何图像。 / p>
换句话说,如何根据我给出的两个脚本,使Second Screen中显示的图像自动更新。谢谢
答案 0 :(得分:0)
在ScreenTwo类中,在类级别创建一个StringProperty:
class ScreenTwo(Screen): #This is where all the functions are
img_src = StringProperty('C:/Users/Jason/Desktop/RaspberryPiTransferred/TEST.jpg')
def command(self, input):
在你的kv文件中引用它:
Image:
source: root.img_src
然后只需执行以下操作即可更改图像:
img_src = 'some/new/image.jpg'
图像应该更新,无需任何进一步的操作。