Python 为什么赢了';我的Kivy程序不会在我告诉它时更新字体大小吗?

Python 为什么赢了';我的Kivy程序不会在我告诉它时更新字体大小吗?,python,python-3.x,user-interface,kivy,kivy-language,Python,Python 3.x,User Interface,Kivy,Kivy Language,我正在做一个选择你自己的冒险游戏,但有时我需要改变字体大小,而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

我正在做一个选择你自己的冒险游戏,但有时我需要改变字体大小,而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'
:
方向:“垂直”
#创建根布局的背景色
在以下情况之前:
颜色:
rgba:0,0,0,1#黑色
矩形:
pos:self.pos
大小:self.size
#此自定义按钮允许字体大小随窗口动态更改
:
字体大小:self.height/3
哈利恩:“中心”
valign:“中心”
文本大小:self.size
尺寸:0.14
:
盒子布局:
方向:“垂直”
标签:
id:文本框
字体大小:self.height/6
文本大小:self.size允许文本换行
哈利恩:“中心”
valign:“中心”
尺寸:0.6
我的按钮:
id:按钮1
文字:“播放”

只有当我从
.kv
中删除
font\u size
时,我才能在
中更改
font\u size
。在运行
\uuu init\uuu
之后,它似乎从
.kv
获取值,这就产生了问题。还有另一个问题:
中的
高度
(和
宽度
)是
100
,而不是预期的大小。它可能在运行
\uuuu init\uuuu
后计算它


在我在Reddit上找到的互联网上搜索:

它使用
时钟
在所有更新之后运行某些函数,并在此函数中更改值

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  # <---
                   

从kivy.clock导入时钟
我可以在
\uuuu init\uuuu
中更改
字体大小
,只要我从
.kv
中删除
字体大小
-运行
\uu init\uuuu
后,它可能会从
.kv
中获取值。但是还有另一个问题-
中的高度
100
-可能是在运行
\uuuuuu init
后计算出来的