2017-01-19 77 views
0

我正在使用python编写我的第一个Kivy应用程序(我现在正在避免使用kv)。我创建了一个名为WorldviewWidget的自定义小部件,我试图将其用作绘制的地方。通过按钮小工具,我只需提供一个size_hint和一个pos_hint,然后它们就会显示出我想要的位置。但是对于我的小部件,我不知道如何使用size_hint和position_hint来调整我在WorldviewWidget中绘制的矩形的大小。这是代码。提前致谢!在Kivy应用程序中自动调整画布

#! /usr/bin/python 
from kivy.app import App 
from kivy.graphics import * 
from kivy.core.window import Window 
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.widget import Widget 
from kivy.uix.button import Button 


class WorldviewWidget(Widget): 
    def __init__(self, **kwargs): 
     super(WorldviewWidget, self).__init__(**kwargs) 

     self.canvas.clear() 
     print self.size, self.pos 

     with self.canvas: 
      Color(1, 0, 0, 1, mode='rgba') 
      # HELP! I want the rectangle to be resized when the window changes size so that it always takes up the same proportion of the screen. 
      self.rect = Rectangle(size=???, pos=???)  


class JFROCS_App(App): 

    def build(self): 
     Window.clearcolor = [1,1,1,1] 
     parent = FloatLayout(size=Window.size) 

     worldview = WorldviewWidget(size_hint=(0.4, 0.4), pos_hint = {'x':0.2, 'y':0.2}) 
     parent.add_widget(worldview) 


     start_btn = Button(text='Start', size_hint=(0.1, 0.1), pos_hint={'x':.02, 'y':.7}, background_color=[0,1,0,1]) 
     start_btn.bind(on_release=self.start_simulation) 
     parent.add_widget(start_btn) 

     pause_btn = Button(text='Pause', size_hint=(0.1,0.1), pos_hint={'x':.02, 'y':.6}, background_color=[1,1,0,1]) 
     pause_btn.bind(on_release=self.pause_simulation) 
     parent.add_widget(pause_btn) 

     stop_btn = Button(text='Stop', size_hint=(0.1,0.1), pos_hint={'x':.02, 'y':.5}, background_color=[1,0,0,1]) 
     stop_btn.bind(on_release=self.stop_simulation) 
     parent.add_widget(stop_btn) 

     return parent 

    def start_simulation(self, obj): 
     print "You pushed the start button!" 
    def pause_simulation(self, obj): 
     print "You pushed the pause button!" 
    def stop_simulation(self, obj): 
     print "You pushed the stop button!" 

if __name__ == '__main__': 
    JFROCS_App().run() 

回答

0

我认为这样的任务是预先为kivy语言,但这里是Python的解决方案。基本上,我已经使用bind-方法使您的小部件监听其父节点size中的更改。有关此机制的更多信息,请参阅this

from kivy.app import App 
from kivy.graphics import * 
from kivy.core.window import Window 
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.widget import Widget 
from kivy.uix.button import Button 


class WorldviewWidget(Widget): 
    def __init__(self, **kwargs): 
     super(WorldviewWidget, self).__init__(**kwargs) 

     self.canvas.clear() 
     print self.size, self.pos 

     with self.canvas: 
      Color(1, 0, 0, 1, mode='rgba') 
      # *changed* ############################################## 
      self.rect = Rectangle(size=self.size, pos=self.pos)  

    # *new* ########################################################## 
    def update_size(self, instance, new_size): 
     print "UPDATING SIZE", instance, new_size 
     self.size[0] = new_size[0] * 0.4 
     self.size[1] = new_size[1] * 0.4 
     self.rect.size = self.size 
     self.pos[0] = self.parent.size[0] * 0.2 
     self.pos[1] = self.parent.size[1] * 0.2 
     self.rect.pos = self.pos 

class JFROCS_App(App): 

    def build(self): 
     Window.clearcolor = [1,1,1,1] 
     parent = FloatLayout(size=Window.size) 

     # *changed* ################################################## 
     worldview = WorldviewWidget(size=(0.4*parent.size[0], 0.4*parent.size[1]), 
            pos=(0.2*parent.size[0], 0.2*parent.size[1])) 
     # makes sure that the widget gets updated when parent's size changes: 
     parent.bind(size=worldview.update_size) 
     parent.add_widget(worldview) 


     start_btn = Button(text='Start', size_hint=(0.1, 0.1), pos_hint={'x':.02, 'y':.7}, background_color=[0,1,0,1]) 
     start_btn.bind(on_release=self.start_simulation) 
     parent.add_widget(start_btn) 

     pause_btn = Button(text='Pause', size_hint=(0.1,0.1), pos_hint={'x':.02, 'y':.6}, background_color=[1,1,0,1]) 
     pause_btn.bind(on_release=self.pause_simulation) 
     parent.add_widget(pause_btn) 

     stop_btn = Button(text='Stop', size_hint=(0.1,0.1), pos_hint={'x':.02, 'y':.5}, background_color=[1,0,0,1]) 
     stop_btn.bind(on_release=self.stop_simulation) 
     parent.add_widget(stop_btn) 

     return parent 

    def start_simulation(self, obj): 
     print "You pushed the start button!" 
    def pause_simulation(self, obj): 
     print "You pushed the pause button!" 
    def stop_simulation(self, obj): 
     print "You pushed the stop button!" 

if __name__ == '__main__': 
    JFROCS_App().run() 

而且具有看看kivy语言 - 它需要对你所有的结合:)

+0

谢谢大家关心!我一定会研究这种语言。我只想了解它在幕后做了什么:) –