python - 为什么我告诉我的 Kivy 程序不更新字体大小?
问题描述
我正在选择你自己的冒险游戏,但有时我需要更改字体大小,而 Kivy 并没有给我预期的结果。这是完整的代码,所以请随意运行它,看看我的意思。这是python文件:
# A Choose your own adventure game
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.lang import Builder
global root
root = BoxLayout() #If I don't define root immediately the program won't work
#root is given a proper definition in class Main()
#Easily add new pages to the program
def add_page(pagenum):
root.clear_widgets()
root.add_widget(pagenum)
#The main window that encapsulates all other widgets
class RootBoxLayout(BoxLayout):
def __init__(self, **kwargs):
super(RootBoxLayout, self).__init__(**kwargs)
# The Menu that drives the game
class Menu(BoxLayout):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
# The Main Menu
class StartMenu(Menu):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
#Text Box
self.ids.textbox.text = "Opening Screen"
# Button 1
self.ids.button1.text = "Play"
self.ids.button1.bind(on_press = self.nextpage1)
def nextpage1(self, *args):
add_page(HappyBee())
class HappyBee(Menu):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
#############################################
### This is where the problem seems to be ###
#############################################
self.ids.textbox.font_size = self.ids.textbox.height/10 #Kivy says nah I don't feel like doing this
self.ids.textbox.text = "This is a very large block of text that I would like " \
"to decrease the font size of. Pressing the button below changes it but I don't " \
"want users to have to press a button just to get the game to function " \
"how it should function from the start."
# Button 1
self.ids.button1.text = "y tho"
self.ids.button1.bind(on_press = self.nextpage1)
# What to do when each button is pressed
def nextpage1(self, *args):
self.ids.textbox.font_size = self.ids.textbox.height/10 # Kivy says ok I can change it now lol
# An App class that will be used to umbrella everything else in the application
class Main(App):
def build(self):
Builder.load_file("cyoa.kv")
global root # Other classes and functions need to easily access root
root = RootBoxLayout()
first_screen = StartMenu()
add_page(first_screen) # Add the Main Menu to the root window
return root
if __name__ == '__main__':
Main().run()
这是对应的kv文件,我保存为cyoa.kv
<RootBoxLayout>:
orientation: 'vertical'
# Create the background color of the root layout
canvas.before:
Color:
rgba: 0,0,0,1 # black
Rectangle:
pos: self.pos
size: self.size
# This custom button allows to font size to change dynamically with the window
<MyButton@Button>:
font_size: self.height/3
halign: 'center'
valign: 'center'
text_size: self.size
size_hint_y: 0.14
<Menu>:
BoxLayout:
orientation: 'vertical'
Label:
id: textbox
font_size: self.height/6
text_size: self.size # Allows text to wrap
halign: 'center'
valign: 'center'
size_hint_y: 0.6
MyButton:
id: button1
text: 'Play'
解决方案
只有从font_size
. _ _ 似乎它在运行后获得了价值,这会产生问题。还有其他问题:(and ) in不是预期的大小。可能它在运行后计算它。__init__
font_size
.kv
.kv
__init__
height
width
__init__
100
__init__
我在 Reddit 上找到的互联网搜索: 如何使用 init 获取屏幕 ID?
它用于Clock
在所有更新后运行某些函数,并在此函数中更改值。
def __init__(self, **kwargs):
#super(...)
Clock.schedule_once(self._do_setup)
def _do_setup(self, *l):
self.ids.something = '....'
在您的代码中
from kivy.clock import Clock # <---
class HappyBee(Menu):
def __init__(self, **kwargs):
super(Menu, self).__init__(**kwargs)
self.ids.textbox.text = "This is a very large block of text that I would like " \
"to decrease the font size of. Pressing the button below changes it but I don't " \
"want users to have to press a button just to get the game to function " \
"how it should function from the start."
self.ids.button1.text = "y tho"
self.ids.button1.bind(on_press = self.nextpage1)
Clock.schedule_once(self.on_init_complete) # <---
def on_init_complete(self, *args, **kwargs):
self.ids.textbox.font_size = self.ids.textbox.height/10 # <---
它可以工作,但有一个小问题 - 它以原始大小显示文本几毫秒。但如果你不知道这一点,那么你可能不会注意到这一点。
编辑:类似的问题:How to Load Kivy IDs Before Class Method is Initialized (Python with Kivy)
推荐阅读
- java - 写一个动态栈链表java的push方法
- php - 将数组从 codeigniter 控制器传递到 /assets 中的文件
- python - youtube-dl 提取的视频描述不包含换行符并且被截断
- jquery - 使用 jQuery ajax load() 方法避免页面刷新
- c - 未找到正确显示异常结果的元音
- c++ - 在匿名类中返回 *this 时,“自动”返回类型的类型是什么?
- php - 如何将自定义站点配置添加到 silverstip?
- javascript - 如何更改滑块的光标?
- javascript - 获取点击了哪个按钮的项目的id reactjs javascript
- c - 如何从 C 中的缓冲区末尾重新读取几个字符?