問(wèn)題描述
所以我正在使用 Kivy GUI 編寫程序,我真的不想使用 .kv 文件,而是將其全部寫入 python 文件.問(wèn)題是在 MainScreen
類中,我想添加一些標(biāo)簽,但我無(wú)法根據(jù)窗口大小的變化使它們的文本換行.當(dāng)我嘗試 self.size
時(shí),我只得到 100x100.我已經(jīng)嘗試了 Kivy 教程書中的所有建議,但沒(méi)有任何效果.我知道我可以使用
使文本多行或設(shè)置標(biāo)準(zhǔn)標(biāo)簽大小,但這不是一個(gè)真正的解決方案.我需要標(biāo)簽大小和文本來(lái)跟隨整個(gè)窗口的變化和要換行的文本.
So I am writting a program using the Kivy GUI and I really don't want to use a .kv file but write it all in the python file. The problem is that inside the MainScreen
Class i want to add some Labels but i cannot make their text to wrap according to window size changes. When i try self.size
I only get 100x100. I have tried all the suggestions from the Kivy tutorial book but nothing worked. I know i could just make the text multiline using
or set a standard Label size, but that wouldn't be a real solution. I need the Label size and text to follow the whole window changes and the text to be wrapped.
我已經(jīng)簡(jiǎn)化了程序,只關(guān)注這個(gè)問(wèn)題.
I have simplified the program to focus just on this issue.
代碼如下:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Rectangle, Color
class MainScreen(FloatLayout):
"""MAIN WINDOW CLASS"""
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
with self.canvas.before:
Color(0.988, 0.725, 0.074, 1, mode='rgba')
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(size=self.update_rect)
#TITLE LABEL
self.add_widget(Label(text="A very Long Sentence that needs to be wrapped.",
bold = True,
font_size="20sp",
pos_hint={'center_x': 0.5, 'center_y': .85},
size_hint=(None, None),
halign="center",
color=(0.055, 0.235, 0.541, 1)))
def update_rect(self, *args):
"""FUNCTION TO UPDATE THE RECATANGLE OF CANVAS TO FIT THE WHOLE SCREEN OF MAINSCREEN ALWAYS"""
self.rect.pos = self.pos
self.rect.size = self.size
class main(App):
"""BUILDING THE APP"""
def build(self):
return MainScreen()
if __name__ == "__main__":
main().run()
謝謝.
推薦答案
所以在對(duì)我的問(wèn)題進(jìn)行了深思熟慮并嘗試了不同的方法之后,我最終得到了一個(gè)合適的僅 python"解決方案.
So after giving my problem a lot of thought and experimenting with different approaches i ended up with a proper "just python" solution.
首先我的回答是基于 1) 官方 Kivy 教程建議,2) 畫布方法的常用操作和 3) 對(duì)變量范圍的謹(jǐn)慎處理.
First of all my answer is based on 1) the official Kivy Tutorials suggestions, 2) the usual manipulation of canvas approach and 3) careful handling on variable scope.
因此標(biāo)簽設(shè)置類似于 Kivy 教程書中建議的設(shè)置.需要有一個(gè)函數(shù)來(lái)更新標(biāo)簽位置及其文本大小(此處為 setting_function
)以與標(biāo)簽大小綁定.此外,我將 Label 分配給一個(gè)變量,以便以后可以輕松引用它.
So the Label settings are similar to the ones suggested by the Kivy tutorial book. There needs to be a function that updates the Label position and it's text size (the setting_function
here) to bind with the Label size. Also I assigned the Label to a variable so I could easily refer to it later.
在所有這些更改之后,我的代碼如下所示:
After all those changes my code looks like this:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.graphics import Rectangle, Color
class MainScreen(FloatLayout, Label):
"""MAIN WINDOW CLASS"""
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
with self.canvas.before:
Color(0.988, 0.725, 0.074, 1, mode='rgba')
self.rect = Rectangle(pos=self.pos, size=self.size)
self.bind(size=self.update_rect)
#TITLE LABEL
self.titlos = Label(text="A very Long Sentence that needs to be wrapped.",
bold = True,
text_size=(None,None),
font_size="20sp",
pos_hint={'center_x': 0.5, 'center_y': .85},
size_hint_y=None,
size = self.size,
height=self.texture_size[1],
halign="center",
valign = "middle",
color=(0.055, 0.235, 0.541, 1))
self.add_widget(self.titlos)
self.titlos.bind(size=self.setting_function)
def setting_function(self, *args):
"""FUNCTION TO UPDATE THE LABEL TO ADJUST ITSELF ACCORDING TO SCREEN SIZE CHANGES"""
self.titlos.pos_hint = {'center_x': 0.5, 'center_y': .85}
self.titlos.text_size=self.size
def update_rect(self, *args):
"""FUNCTION TO UPDATE THE RECATANGLE OF CANVAS TO FIT THE WHOLE SCREEN OF MAINSCREEN ALWAYS"""
self.rect.pos = self.pos
self.rect.size = self.size
class main(App):
"""BUILDING THE APP"""
def build(self):
return MainScreen()
if __name__ == "__main__":
main().run()
上面的代碼確實(shí)符合我在問(wèn)題中設(shè)置的要求.很容易將它用于您自己的項(xiàng)目.只需注意變量的范圍并使用大量打印消息進(jìn)行檢查.
The above code does meet the requirements I set in my question. It is easy to use it to your own projects. Just take care of the variables' scopes and use lots of print messages for checking.
最后我想只使用 Python 來(lái)解決這個(gè)問(wèn)題(而不僅僅是使用 kivy 語(yǔ)言來(lái)處理這些問(wèn)題就像小菜一碟)的原因是我想要在運(yùn)行時(shí)動(dòng)態(tài)更改變量,即用作標(biāo)簽參數(shù).而且我只需要找出答案,因?yàn)槲液芄虉?zhí).
Finally the reason I wanted to use just Python to solve this problem (and not just use kivy language which handles those issues like a piece of cake) is that I want to have variables that are dynamically changed during the run time, that are used as the Label parameters. And also I just had to find out because I am stubborn.
謝謝.
這篇關(guān)于包裝 Kivy 標(biāo)簽的文本的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!