[英]Kivy: How to place canvas in a specific layout
我正在嘗試將圖像放置在自動每秒更新的畫布上。 畫布具有圖像的特定來源,每當替換圖像文件時,我都會對其進行更新並重新加載到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屏幕 。
圖像顯示在屏幕的左下方。
當我按下“拍照鍵”,它的更新,在這個屏幕截圖顯示圖像Pic拍攝后
但是,我沒有想要的位置控制權。
最初,我使用不同的代碼將圖像顯示在屏幕的右上角,但是我無法動態更新圖像。 較舊的GUI位置我以前的代碼在錨點布局中設置了該圖像,但它是靜態的。 現在,我正在使用畫布,它會使用當前代碼進行更新,但是無法像以前那樣放置它。
我設置“ 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>
您可以使用水平方向(默認)的另一個BoxLayout:
<ScreenTwo>:
BoxLayout:
BoxLayout:
size_hint_x: .5
spacing: 10
padding: 10
orientation: "vertical"
...
AnchorLayout:
anchor_x: 'right'
anchor_y:'top'
MyPicture:
size_hint_y: .5
withis代碼我得到輸出:
但真誠地,我更喜歡使用floatlayout並自行調整pos和大小
浮動布局是這樣的:
<ScreenTwo>:
BoxLayout:
BoxLayout:
size_hint_x: .5
spacing: 10
padding: 10
orientation: "vertical"
...
FloatLayout:
size_hint_x: .5
MyPicture:
pos_hint: {'center_x': .5, 'y': .5} #edit this like you want
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.