![](/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.