简体   繁体   中英

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.

I now want to recode this in Kivy but I have some problems understanding the change in philosophy.

The Tkinter skeleton is

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.)

In Kivy I want to change the design by creating several FloatLayout widgets, equivelent to the tk.Label above. 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:

#: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)?

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. That's why your app is blank, it's just a Dashboard widget that is itself blank.

You instead need to add the Time widget to the dashboard, eg in the __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:

<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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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