![](/img/trans.png)
[英]porting a python3 app from Linux to Android using kivy and bluepy
[英]porting a fullscreen app from Tkinter to Kivy
我在Tkinter
写了一个仪表板应用程序,基本上是一个全屏应用程序,在网格中有几个tk.Label
,用各种信息更新。
我现在想在Kivy
重新编写这个,但我在理解哲学的变化时遇到了一些问题。
Tkinter
骨架是
class Dashboard(object):
def __init__(self, parent):
self.root = parent.root
self.timestr = tk.Label(self.root)
self.timestr.configure(...)
(...)
然后我.configure()
各种东西(字体,文本表等)
在Kivy
我想通过创建几个FloatLayout
小部件来改变设计,这些小部件与上面的tk.Label
。 我到目前为止
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()
与相关的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"
启动应用程序后,它会全屏显示但是为空。
我应该如何表达我想要实例化Dashboad()
然后在其中包含一些小部件(例如Time()
的事实?
class Dashboard(Widget):
Time()
我认为你对这是做什么有误解 - 没有。 Time
对象已实例化,但未添加到Dashboard
或任何其他对象。 这就是为什么你的应用程序是空白的,它只是一个Dashboard
小部件本身是空白的。
您需要将Time
窗口小部件添加到仪表板,例如__init__
:
class Dashboard(Widget):
def __init__(self, **kwargs):
super(Dashboard, self).__init__(**kwargs)
self.add_widget(Time())
由于您总是希望这样做,因此使用kv规则更容易也更好:
<DashBoard>:
Time:
你现在也会有一些混乱的定位,但看起来你还在试验它。
而不是标签center_x
是self.width/2
,我认为是指标签本身,请尝试root.width/2
,我相信它是指根小部件,在本例中是Time
。
我很确定,在kv文件中, root
通常是指这些<>
之间的小部件(这是你当时调整的任何东西的根父), self
指的是当前小部件, app
指的是应用实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.