如何在Python中将我的应用程序上的工具栏与Kivy集成在一起?

时间:2019-02-13 20:17:03

标签: python kivy

我今天来找你是因为几天来我一直无法解决我的问题。

我要做什么:

我想在应用程序的侧面创建一个工具栏,并使其在所有屏幕上都可用。 我尝试了几件事,但没有任何结果:(我将把一些代码留给您看我做了什么。

[我在这里删除了代码,因为我在下面进行了更改]

在此先感谢您的帮助,我们将提供更多信息。 祝你有美好的一天!

新闻:

我只是尝试执行被建议使用@Erik的操作,但是启动程序时出现错误。

错误

Traceback (most recent call last):
   File "/Users/maelito/Desktop/tradingApp/app.py", line 1, in <module>
     from gui import window as w
   File "/Users/maelito/Desktop/tradingApp/gui/__init__.py", line 4, in <module>
     app.run()
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/app.py", line 799, in run
     self.load_kv(filename=self.kv_file)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/app.py", line 596, in load_kv
     root = Builder.load_file(rfilename)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/builder.py", line 301, in load_file
     return self.load_string(data, **kwargs)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/builder.py", line 368, in load_string
     parser = Parser(content=string, filename=fn)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 392, in __init__
     self.parse(content)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 498, in parse
     self.execute_directives()
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 452, in execute_directives
     kivy.lang.builder.Builder.load_file(ref)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/builder.py", line 301, in load_file
     return self.load_string(data, **kwargs)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/builder.py", line 368, in load_string
     parser = Parser(content=string, filename=fn)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 392, in __init__
     self.parse(content)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 501, in parse
     objects, remaining_lines = self.parse_level(0, lines)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 605, in parse_level
     level + 1, lines[i:], spaces)
   File "/Users/maelito/env/lib/python3.7/site-packages/kivy/lang/parser.py", line 664, in parse_level
     if current_property[:3] == 'on_':
 TypeError: 'NoneType' object is not subscriptable

因此,我也与您分享了新代码。 (我在screen1.kv和screen2.kv中什么都没有改变。)

Window.py

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen
from kivy.factory import Factory
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager


class ToolBar(GridLayout):
    pass


class Screen1(Screen):
    pass


class Screen2(Screen):
    pass


class WindowInterface(GridLayout):
    manager = ScreenManager()

    toolbar = ToolBar()
screen_1 = Screen1()
screen_2 = Screen2()

screen_1.add_widget(toolbar)
screen_2.add_widget(toolbar)

manager.add_widget(screen_1)
manager.add_widget(screen_2)

    def construct_toolbar(self):
        self.toolbar.add_buttons(self)


class Window(App):
    pass

window.kv

#:include gui/kv_files/screen_1.kv
#:include gui/kv_files/screen_2.kv
#:include gui/kv_files/toolbar.kv

#:import NoTransition kivy.uix.screenmanager.NoTransition

<WindowInterface>
    name: 'main_screen'
    BoxLayout:
            orientation: "vertical"

            Label:
                text: "main screen"
            Button:
                text: "to screen 1"
                on_press: app.root.current = "screen_1"
            Button:
                text: "to screen 2"
                on_press: app.root.current = "screen_2"

    ScreenManager:
        Screen1:

        Screen2:

        ToolBar:

toolbar.kv

<Toolbar@GridLayout>
BoxLayout:
    orientation: "vertical"
    size: 10,100

    canvas:
        Color:
            rgba: (1,1,1,1)
        Rectangle:
            size: self.size
            pos: self.pos

先谢谢您。 Maëlito!

2 个答案:

答案 0 :(得分:0)

我注意到您正在创建从.kv文件(custom_scp() { local user port # avoid polluting namespace outside your function port=$1; shift # assign to a local variable, then pop off the argument list user=$1; shift # repeat scp -P "$port" -r "$@" "${user}@myserver.com:~/" } )中的GridLayout继承的工具栏,但是由于它是一个类,因此您缺少冒号和缩进。看起来应该像这样:

Toolbar@GridLayout

尝试一下,让我知道会发生什么。不知道这是否是完整的问题,只是我在您的代码中注意到的问题。

答案 1 :(得分:0)

这是您要完成的任务的最小示例。希望您能看到它的内部工作原理,并了解如何使其适合您的应用程序!

main.py

from kivy.app import App
from kivy.uix.screenmanager import Screen

class ToolBarGrid():
    pass

class Screen1(Screen):
    pass

class Screen2(Screen):
    pass

class MainApp(App):
    def load_screen(self, new_screen_name):
        self.root.ids.screen_manager_id.current = new_screen_name

MainApp().run()

main.kv

GridLayout:
    cols: 1
    ToolBarGrid:
    ScreenManager:
        id: screen_manager_id
        Screen1:
            id: "screen1"
            name: "screen1"
        Screen2:
            id: "screen2"
            name: "screen2"

<ToolBarGrid@GridLayout>:
    rows: 1
    Label: 
        text: "one"
    Label: 
        text: "two"
    Label: 
        text: "three"
    Label: 
        text: "four"

<Screen1>:
    GridLayout:
        cols: 1
        Label:
            text: "Screen 1"
        Button: 
            text: "Load screen 2"
            on_release: app.load_screen("screen2")

<Screen2>:
    GridLayout:
        cols: 1
        Label:
            text: "Screen 2"
        Button: 
            text: "Load screen 1"
            on_release: app.load_screen("screen1")