简体   繁体   English

将全屏应用程序从Tkinter移植到Kivy

[英]porting a fullscreen app from Tkinter to Kivy

I wrote a dashboard application in Tkinter , basically a fullscreen app with a few tk.Label in a grid, updated with various information. 我在Tkinter写了一个仪表板应用程序,基本上是一个全屏应用程序,在网格中有几个tk.Label ,用各种信息更新。

I now want to recode this in Kivy but I have some problems understanding the change in philosophy. 我现在想在Kivy重新编写这个,但我在理解哲学的变化时遇到了一些问题。

The Tkinter skeleton is Tkinter骨架是

class Dashboard(object):
    def __init__(self, parent):
        self.root = parent.root
        self.timestr = tk.Label(self.root)
        self.timestr.configure(...)
(...)

I then .configure() various things (font, text tabel, etc.) 然后我.configure()各种东西(字体,文本表等)

In Kivy I want to change the design by creating several FloatLayout widgets, equivelent to the tk.Label above. Kivy我想通过创建几个FloatLayout小部件来改变设计,这些小部件与上面的tk.Label I have so far 我到目前为止

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window

class Time(Widget):
    def __init__(self):
        self.time = "xx:xx"

    def update(self):
        self.time = "9:53"

class Dashboard(Widget):
    Time()

class DashApp(App):
    def build(self):
        dash = Dashboard()
        return dash

Window.fullscreen = True
DashApp().run()

with the relavant kv file: 与相关的kv文件:

#:kivy 1.8.0
<Time>:
    size: root.width, root.height / 4
    pos: 0, 0
    Label:
        center_x: self.width / 2
        top: self.top - 5
        font_size: 70
        text: "aaa"

Upon launching the app it goes fullscreen but is empty. 启动应用程序后,它会全屏显示但是为空。

How should I express the fact that I want to instantiate a Dashboad() and then within it some widgets ( Time() for instance)? 我应该如何表达我想要实例化Dashboad()然后在其中包含一些小部件(例如Time()的事实?

class Dashboard(Widget):
    Time()

I think you have a misconception about what this does - that being, nothing. 我认为你对这是做什么有误解 - 没有。 The Time object is instantiated but not added to the Dashboard or anything. Time对象已实例化,但未添加到Dashboard或任何其他对象。 That's why your app is blank, it's just a Dashboard widget that is itself blank. 这就是为什么你的应用程序是空白的,它只是一个Dashboard小部件本身是空白的。

You instead need to add the Time widget to the dashboard, eg in the __init__ : 您需要将Time窗口小部件添加到仪表板,例如__init__

class Dashboard(Widget):
    def __init__(self, **kwargs):
        super(Dashboard, self).__init__(**kwargs)
        self.add_widget(Time())

Since you always want to do this, it's even easier and better to do it with a kv rule: 由于您总是希望这样做,因此使用kv规则更容易也更好:

<DashBoard>:
    Time:

You'll also have some messed up positioning right now, but it looks like you're still experimenting with that. 你现在也会有一些混乱的定位,但看起来你还在试验它。

Instead of the Label center_x being self.width/2 , which I think refers to the label itself, try root.width/2 , which I believe refers to the root widget, in this case, Time . 而不是标签center_xself.width/2 ,我认为是指标签本身,请尝试root.width/2 ,我相信它是指根小部件,在本例中是Time

I'm quite sure, that in the kv file, root generally refers to the widget between these <> (that is the root parent of whatever you are tweaking at the time), self refers to the current widget, and app refers to the app instance. 我很确定,在kv文件中, root通常是指这些<>之间的小部件(这是你当时调整的任何东西的根父), self指的是当前小部件, app指的是应用实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM