【发布时间】:2018-05-17 15:42:52
【问题描述】:
我正在尝试将图像放置在每秒自动更新的画布上。画布有一个特定的图像来源,每当图像文件被替换时,我都会更新它并在 GUI 上重新加载。无需用户输入,也无需退出 GUI。
我能够让图像重新加载,但现在我无法将图像放置在我想要的位置。我希望能够把它放在一个布局中,最好位于屏幕的右上角
我的以下主要python文件如下
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.anchorlayout import AnchorLayout
from kivy.uix.widget import Widget
from kivy.graphics.instructions import InstructionGroup
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
class MyPicture(Widget):
def __init__(self,**kwargs):
super(MyPicture,self).__init__(**kwargs)
with self.canvas:
MyPicture.picture = Image(source = 'C:/Users/Jason/Desktop/RaspberryPiTransferred/TEST.jpg',
size_hint=(.3,.3))
#self.add_widget(Image(MyPicture.picture))
def update(self):
MyPicture.picture.reload()
Clock.schedule_interval(update,1)
#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")
MyPicture
我得到的是以下 GUI 屏幕GUI Screen。
图像显示在屏幕的左下方。
当我按下“拍照按钮”时,它会更新,如截图所示Image after pic is taken
但是我无法像我想要的那样控制展示位置。
最初我用不同的代码将图像显示在屏幕的右上角,但我无法动态更新图像Older GUI placement 我之前的代码将该图像设置为锚布局,但它是静态的。现在我正在使用画布,它会使用我当前的代码进行更新,但我不能像以前那样放置它。
我设置“size_hint”的部分没有做任何事情,我只是在猜测和检查这一点。
有没有更简单的方法可以在 KV 类中调用该 Picture 类,以便在特定布局中设置图像?例如
AnchorLayout:
anchor_x: 'right'
anchor_y: 'left'
BoxLayout:
spacing: 10
padding: 10
Image:
<Dynamic image goes here>
我在哪里而不是“动态图像到这里”
source: <location of image>
【问题讨论】:
标签: python image canvas layout kivy