2017-12-04 48 views
0

毎秒自動的に更新されるキャンバスに画像を配置しようとしています。キャンバスにはイメージの特定のソースがあり、イメージファイルが置き換えられるたびに、GUI上でイメージファイルを更新して再ロードします。ユーザー入力がなく、GUIを終了する必要もありません。Kivy:特定のレイアウトにキャンバスを配置する方法

イメージを再ロードすることができましたが、今は自分が望む場所にイメージを配置できません。私は

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ファイルが

の下に示されているが、次のように私の次の主要なPythonのファイルは、好ましくは、画面の右上にある、レイアウトにそれを置くことができるようにしたいです
#: import os os 
<[email protected]>: 
    font_size: 12 
    size_hint: .2,.1 

<[email protected]>: 
    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") 


    MyPicture 

次のGUI画面はGUI Screenです。

画像は画面の左下に表示されます。

私は「絵のボタンを撮る」を押すと、このスクリーンショットでImage after pic is taken

を示すように、それは私がしたいように、私は配置を制御できませんが更新されます。

もともと私はイメージが異なるコードで、画面の右上に表示されていたが、私はイメージを更新することができませんでした動的Older GUI placement 私の以前のコードでは、アンカーレイアウトにそのイメージを設定し、それは静的でした。キャンバスを使用しているので、現在のコードで更新されますが、前と同じように配置することはできません。

私が「size_hint」を設定した部分は何もしません。私はこの時点で推測していました。

画像が特定のレイアウトに設定されるように、KVクラスのピクチャクラスを呼び出す簡単な方法はありますか?私は、コードwithis

<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 

:私は

source: <location of image> 

答えて

0

を持っていた "ダイナミック画像をここに" の代わりにあなたが水平方向(デフォルト)の向きと、別のBoxLayoutを使用することができますたとえば

AnchorLayout: 
    anchor_x: 'right' 
    anchor_y: 'left' 
    BoxLayout: 
     spacing: 10 
     padding: 10 
     Image: 
      <Dynamic image goes here> 

出力を取得します。

output

けどsincerily私は、フロートレイアウトでこのような何か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 
+0

を私は私のKVファイルにあることを追加しましたが、私は静止画で終わります画面の左下に表示されます – Jason

+0

コードでは、ソースを持つ画像ファイルがあることを示しています。これは静的であることを意味します。毎秒更新するにはそのイメージファイルが必要です。私のコードはイメージを取り、そのソースのイメージファイルを置き換えます。 あなたが示した方法を使用しましたが、画像ファイルが更新されるようにする方法がわかりません – Jason

+0

@Jason「静的な」画像でこのコードを試してみてください。それは動的な画像を持っています –

関連する問題