Kivy,python:在放大或缩小 window 时动态调整文本大小
Kivy, python: Dynamically resize text when zooming window in or out
下面列出了我的代码。当 运行 应用程序时,我希望在“缩放”in/out 应用程序 window(拖动 window 变大或变小)时动态地使文本大小变大变小。就像按钮一样(白色方块)。现在文本大小保持不变。
注意:
如果您复制我的代码进行测试,请在与 .py 文件相同的文件夹中下载 blue.png。
如有任何帮助,我们将不胜感激!
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class FirstScreen(Screen):
pass
class myScreenManager(ScreenManager):
pass
class ImageButton(ButtonBehavior, Image):
pass
root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
myScreenManager:
transition: FadeTransition()
FirstScreen:
<FirstScreen>:
name: 'first'
FloatLayout:
orientation: 'vertical'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'blue.png'
ImageButton:
pos_hint: {'x': 0.25, 'y': 0.18}
size_hint: 0.5, 0.3
Label:
text: 'THIS\nIS\nA\nTEST'
text_size: root.width, None
font_size: 40
pos_hint: {'x': 0, 'y': 0.19}
halign: 'center'
''')
class myScreenManagerApp(App):
def build(self):
return root_widget
myScreenManagerApp().run()
您可以使 font_size
取决于应用程序的大小 window。尝试替换:
font_size: 40
类似的东西:
font_size: root.height/12
下面列出了我的代码。当 运行 应用程序时,我希望在“缩放”in/out 应用程序 window(拖动 window 变大或变小)时动态地使文本大小变大变小。就像按钮一样(白色方块)。现在文本大小保持不变。
注意: 如果您复制我的代码进行测试,请在与 .py 文件相同的文件夹中下载 blue.png。
如有任何帮助,我们将不胜感激!
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class FirstScreen(Screen):
pass
class myScreenManager(ScreenManager):
pass
class ImageButton(ButtonBehavior, Image):
pass
root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
myScreenManager:
transition: FadeTransition()
FirstScreen:
<FirstScreen>:
name: 'first'
FloatLayout:
orientation: 'vertical'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'blue.png'
ImageButton:
pos_hint: {'x': 0.25, 'y': 0.18}
size_hint: 0.5, 0.3
Label:
text: 'THIS\nIS\nA\nTEST'
text_size: root.width, None
font_size: 40
pos_hint: {'x': 0, 'y': 0.19}
halign: 'center'
''')
class myScreenManagerApp(App):
def build(self):
return root_widget
myScreenManagerApp().run()
您可以使 font_size
取决于应用程序的大小 window。尝试替换:
font_size: 40
类似的东西:
font_size: root.height/12