[英]How to resize label in kivy to the size of my screen?
Hi I have created this kivy app.您好,我创建了这个 kivy 应用程序。 I just want to know how can i resize the label to the size of whole screen.
我只想知道如何将 label 调整为整个屏幕的大小。 And also if i resize the screen with mouse, the label also resizes itself to that screen.
而且,如果我用鼠标调整屏幕大小,label 也会根据该屏幕调整自身大小。 I know that to do this we need to use
size: root.width, root.height
.我知道要做到这一点,我们需要使用
size: root.width, root.height
。 But that is done in the kivy file.但这是在 kivy 文件中完成的。 What if i want to do this in my python file itself?
如果我想在我的 python 文件本身中执行此操作怎么办? Thanks for your suggestions.
感谢您的建议。
Pls refer to the code below:请参考以下代码:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
class MyGrid(Widget):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.inner = GridLayout()
self.inner.cols = 1
self.add_widget(self.inner)
self.inner.add_widget(Label(text = 'Hi!'))
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
MyApp().run()
You can use the Window.height
and Window.width
properties and on_size
method to achieve this.您可以使用
Window.height
和Window.width
属性和on_size
方法来实现这一点。 As shown in the below modified code:如下修改代码所示:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.core.window import Window
class MyGrid(Widget):
def __init__(self, **kwargs):
super(MyGrid, self).__init__(**kwargs)
self.inner = GridLayout()
self.inner.cols = 1
self.add_widget(self.inner)
self.inner.add_widget(Label(text = 'Hi!'))
def on_size(self, *args):
self.inner.height = Window.height
self.inner.width = Window.width
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == '__main__':
MyApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.