2016-07-15 76 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