为什么我的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'

只有当我从.kv中删除font_size时,我才能在__init__中更改font_size。似乎它在运行__init__后从.kv中获取价值,这会产生问题。还有另一个问题:__init__中的height(和width(是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  # <---

它可以工作,但有一个小问题 - 它以原始大小显示文本几毫秒。但如果你不知道这一点,那么你可能不会注意到这一点。

>EDIT:类似的问题:如何在类方法初始化之前加载Kivy ID(Python与Kivy(

最新更新