您可以在弹出窗口 window 或 kivy 屏幕上打印表格 (python)
Can you print tables on a popup window or on a screen in kivy (python)
我有一个程序可以从网页上抓取信息并将其存储在列表中,然后使用 tabulate.tabulate
打印出来。我现在正尝试使用 kivy 将这个程序变成一个更有用的 GUI。然而,我看到的教程只展示了如何使用 Label
在屏幕或弹出窗口上书写,这似乎只打印文本,而不是 tables。所以我不认为我可以在那里打印 table (至少不使用制表)。
所以我的问题是,我正在尝试做的事情是否可行,如果可行,怎么做?
编辑:我尝试过的
.kv 文件
WindowManager:
Screen1:
<Screen1>
name: 'screen1'
Label:
text: tabulate([[1, 2, 3], [2, 4, 6]])
.py 文件
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from tabulate import tabulate
kv = Builder.load_file('my.kv')
class Screen1(Screen):
pass
class WindowManager(ScreenManager):
pass
class MyMainApp(App):
def build(self):
return kv
if __name__ == '__main__':
MyMainApp().run()
您可以使用 kv
文件中设置的 ids
访问 Label
。此外,如果您访问 kv
文件外的 tabulate
,则不再需要 kv
文件内的 import
。这是一个修改后的 kv
文件,它为 Label
添加了一个 id
(并且还添加了一个 Button
来触发 table 构建):
WindowManager:
Screen1:
<Screen1>
name: 'screen1'
Label:
id: table # this is the id
font_name: 'DejaVuSansMono'
pos_hint: {'center_x':0.5, 'top':0.9}
size_hint: None, None
size: self.texture_size
Button:
text: 'Make Table'
pos_hint: {'center_x':0.5, 'y':0.1}
size_hint: None, None
size: self.texture_size
on_release: root.make_table()
以上代码还为 Label
分配了等宽字体。我不能保证你的系统会有那种特定的字体。然后在你的 Screen1
class 中定义 make_table()
方法:
class Screen1(Screen):
def make_table(self):
self.ids.table.text = tabulate([[1, 2, 3], [2, 4, 6]])
我有一个程序可以从网页上抓取信息并将其存储在列表中,然后使用 tabulate.tabulate
打印出来。我现在正尝试使用 kivy 将这个程序变成一个更有用的 GUI。然而,我看到的教程只展示了如何使用 Label
在屏幕或弹出窗口上书写,这似乎只打印文本,而不是 tables。所以我不认为我可以在那里打印 table (至少不使用制表)。
所以我的问题是,我正在尝试做的事情是否可行,如果可行,怎么做?
编辑:我尝试过的
.kv 文件
WindowManager:
Screen1:
<Screen1>
name: 'screen1'
Label:
text: tabulate([[1, 2, 3], [2, 4, 6]])
.py 文件
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from tabulate import tabulate
kv = Builder.load_file('my.kv')
class Screen1(Screen):
pass
class WindowManager(ScreenManager):
pass
class MyMainApp(App):
def build(self):
return kv
if __name__ == '__main__':
MyMainApp().run()
您可以使用 kv
文件中设置的 ids
访问 Label
。此外,如果您访问 kv
文件外的 tabulate
,则不再需要 kv
文件内的 import
。这是一个修改后的 kv
文件,它为 Label
添加了一个 id
(并且还添加了一个 Button
来触发 table 构建):
WindowManager:
Screen1:
<Screen1>
name: 'screen1'
Label:
id: table # this is the id
font_name: 'DejaVuSansMono'
pos_hint: {'center_x':0.5, 'top':0.9}
size_hint: None, None
size: self.texture_size
Button:
text: 'Make Table'
pos_hint: {'center_x':0.5, 'y':0.1}
size_hint: None, None
size: self.texture_size
on_release: root.make_table()
以上代码还为 Label
分配了等宽字体。我不能保证你的系统会有那种特定的字体。然后在你的 Screen1
class 中定义 make_table()
方法:
class Screen1(Screen):
def make_table(self):
self.ids.table.text = tabulate([[1, 2, 3], [2, 4, 6]])