2016-07-15 46 views
1

我想通过实现ComicCreator GUI示例模板作为我自己的项目模板来创建一个GUI。该code是容易做到,但我希望能够每按下一个按钮的时间来重新配置drawingspace.kv,例如说这样的事情:如何用kivy按下按钮时更改空格?

enter image description here

问:我怎么能配置drawingspace.kv对于按下的每个按钮都有不同的小部件布局。

回答

2

一个很好的方法是使用屏幕。

因为我早就已经有了这个应用程序的例子,所以很容易实现这些屏幕,并重新编写了一些类。

当按下按钮时,您将屏幕管理器的当前设置设置为您指定的屏幕名称。

然后,您只需在每个屏幕内,kv文件或python文件中编辑布局。

我选择使用kv语言编写大部分布局。因为我发现以这种方式开发布局的方式更容易。 后来我可以将它重写为python,如果我想要的话。

所以我的Python文件现在看起来是这样的:

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.properties import StringProperty 
from kivy.clock import Clock 
from kivy.uix.screenmanager import Screen,ScreenManager,NoTransition 
from kivy.lang import Builder 
import time 


Builder.load_file("kv.kv") 


class MyLayout(BoxLayout): 

    def __init__(self,**kwargs): 
     super(MyLayout,self).__init__(**kwargs) 
     self.orientation = "vertical" 
     self.padding = 10 


class MainScreen(Screen): 
    pass 


class RemoveScreen(Screen): 
    pass 


class GroupScreen(Screen): 
    pass 


class MyLogo(BoxLayout): 

    your_time = StringProperty() 
    def __init__(self,**kwargs): 
     super(MyLogo,self).__init__(**kwargs) 
     Clock.schedule_interval(self.set_time, 0.1) 

    def set_time(self,dt): 
     self.your_time = time.strftime("%m/%d/%Y %H:%M") 




class MyApp(App): 
    def __init__(self,**kwargs): 
     super(MyApp,self).__init__(**kwargs) 
     self.sm = ScreenManager(transition=NoTransition()) 

     self.sm.add_widget(MainScreen(name = "main")) 
     self.sm.add_widget(RemoveScreen(name = "remove")) 
     self.sm.add_widget(GroupScreen(name = "group")) 

     self.sm.current = "main" 

    def build(self): 
     return self.sm 


if __name__ == "__main__": 
    MyApp().run() 

而且kv.kv文件看起来像这样:

#:kivy 1.9.1 

<[email protected]>: 
    padding: 10,10,10,0 
    spacing: 10 
    size_hint: 1,0.3 
    orientation: "horizontal" 
    Button: 
     text: "Clear" 
     on_press: app.sm.current = "main" 
    Button: 
     text: "Remove" 
     on_press: app.sm.current = "remove" 
    Button: 
     text: "Group" 
     on_press: app.sm.current = "group" 
    Button: 
     text: "Color" 
    Button: 
     text: "Gestures" 

<MyLogo>: 
    spacing: 10 
    padding: 10,10,10,0 
    orientation: "horizontal" 
    BoxLayout: 
     orientation: "vertical" 
     size_hint: 0.3,1 
     canvas: 
      Rectangle: 
       pos: self.pos 
       size: self.size 
     AsyncImage 
      source: 'http://lmsotfy.com/so.png' 
     Label: 
      size_hint: 1,0.3 
      text: root.your_time 
      color: [0,0,0,1] 
     Label: 
      size_hint: 1,0.3 
      text: "NYC, New York, USA" 
      color: [0,0,0,1] 


<MainScreen>: 
    MyLayout: 
     MyLogo: 
      #Button: 
      # text: "main" 

     MyButtons: 
      #buttons 

     BoxLayout: 
      padding: 10,10,10,10 
      size_hint: 1,0.3 
      Button: 
       text: "Total figures: 1   Kivy Started" 


<RemoveScreen>: 
    MyLayout: 
     MyLogo: 
      BoxLayout: 
       orientation: "horizontal" 
       Label: 
        font_size: "40sp" 
        text: "Remove" 
       Button: 
        font_size: "20sp" 
        text: "Remove this or something" 

     MyButtons: 
      #buttons 

     BoxLayout: 
      padding: 10,10,10,10 
      size_hint: 1,0.3 
      Button: 
       text: "Total figures: 1   Kivy Started" 


<GroupScreen>: 
    MyLayout: 
     MyLogo: 
      BoxLayout: 
       orientation: "vertical" 
       Label: 
        font_size: "40sp" 
        text: "Group" 
       Button: 
        font_size: "20sp" 
        text: "Something groups stuff" 

     MyButtons: 
      #buttons 

     BoxLayout: 
      padding: 10,10,10,10 
      size_hint: 1,0.3 
      Button: 
       text: "Total figures: 1   Kivy Started" 
+0

除了[示例]中的文件外,'kv.kv'是一个新文件(https://www.packtpub.com/packtlib/book/Application-Development/9781785286926/1/ch01lvl1sec13/Our%20project%20% 20Comic%20Creator)? – 3kstc

+0

@ 3kstc是的,它被python的第10行载入.py文件'Builder.load_file(“kv.kv”)' – EL3PHANTEN

+0

你是天赐之宝! – 3kstc

0

布局框架应该是a screen manager,并且每个布局a screen。屏幕过渡将通过按下按钮来触发。如果您不知道如何操作,您还可以观看教程here,但文档应该足够了。

+0

谢谢,我其实是有通过它发布之前读....但我不明白*如何*在[代码](https://www.packtpub.com/packtlib/book/Application-Development/9781785286926/1/ch01lvl1sec13/Our%20project%20%20Comic %20Creator)。 – 3kstc