您好,我目前正在 Windows 上使用 kivy 创建 GUI。我用它来 ssh 到 Raspberry Pi,拍摄图像,然后将其发送回我的 Windows 机器。我已经使用 GUI 成功完成了此操作。我有两个屏幕。第一个是登录。登录后,会出现第二个屏幕,其中包含按钮和图像。登录屏幕和按钮正常发挥其功能。但是,图像文件并没有像我想要的那样更新。要么我希望它以一定的间隔更新,要么在我拍照后更新。
这是 。我希望右上角的图片能够自动更新。 “拍照”按钮拍摄照片,然后将其发送到我的计算机,我想在 GUI 中刷新图像并显示它
我的主要 python 文件,我称之为“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 [email protected]: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 文件中的任何图像。
在 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'
图像应该会更新,无需任何进一步操作。
https://kivy.org/doc/stable/api-kivy.uix.image.html
reload()
im = Image(source = '1.jpg')
# -- do something --
im.reload()
# image will be re-loaded from disk