0
我有一個Python/Kivy應用程序,我正在創建,並且我正在嘗試在窗口大小調整時調整文本大小。我查了一下,但沒有找到和我預期的完全一樣的東西。我想我會將on_resize綁定到調整文本大小的函數,但是我收到了很多錯誤。這是相關的代碼。當窗口調整大小時調整文本大小Python/Kivy
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.font_size = 25
class MainApp(App):
def build(self):
def win_cb(window, width, height):
new_size = sqrt(width**2 + height**2)/40.6
MainScreen().font_size = new_size
Window.bind(on_resize=win_cb)
我還要提到的是,在一個.kv文件我有這樣的事情
<MainScreen>:
Label:
text: "Text here"
pos_hint: {"x": -0.15, "y": 0.05}
font_size: root.font_size*80/25
在運行時,它工作正常,但是當我調整窗口的大小,我得到錯誤的巨大列,看樣子與MainScreen相關的沒有屬性font_size。
這是錯誤消息:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1649, in create_handler
return eval(value, idmap)
File "main.kv", line 46, in <module>
font_size: root.font_size
File "kivy/weakproxy.pyx", line 19, in kivy.weakproxy.WeakProxy.__getattr__ (kivy/weakproxy.c:1101)
AttributeError: 'MainScreen' object has no attribute 'font_size'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 2011, in _apply_rule
value, rule, rctx['ids'])
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1654, in create_handler
cause=tb)
kivy.lang.BuilderException: Parser: File "main.kv", line 46:
...
44: text: "Simplify Fraction"
45: pos_hint: {"right": 1, "top":0.2}
>> 46: font_size: root.font_size
47:
48:
...
AttributeError: 'MainScreen' object has no attribute 'font_size'
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1649, in create_handler
return eval(value, idmap)
File "main.kv", line 46, in <module>
font_size: root.font_size
File "kivy/weakproxy.pyx", line 19, in kivy.weakproxy.WeakProxy.__getattr__ (kivy/weakproxy.c:1101)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/onehitfinish/Downloads/Gregulator Files/test.py", line 88, in <module>
MainApp().run()
File "/usr/lib/python3.4/site-packages/kivy/app.py", line 824, in run
runTouchApp()
File "/usr/lib/python3.4/site-packages/kivy/base.py", line 487, in runTouchApp
EventLoop.window.mainloop()
File "/usr/lib/python3.4/site-packages/kivy/core/window/window_sdl2.py", line 525, in mainloop
self._mainloop()
File "/usr/lib/python3.4/site-packages/kivy/core/window/window_sdl2.py", line 290, in _mainloop
EventLoop.idle()
File "/usr/lib/python3.4/site-packages/kivy/base.py", line 327, in idle
Clock.tick()
File "/usr/lib/python3.4/site-packages/kivy/clock.py", line 483, in tick
self._process_events()
File "/usr/lib/python3.4/site-packages/kivy/clock.py", line 615, in _process_events
event.tick(self._last_tick, remove)
File "/usr/lib/python3.4/site-packages/kivy/clock.py", line 374, in tick
ret = callback(self._dt)
File "/usr/lib/python3.4/site-packages/kivy/core/window/window_sdl2.py", line 188, in create_window
super(WindowSDL, self).create_window()
File "/usr/lib/python3.4/site-packages/kivy/core/window/__init__.py", line 779, in create_window
self.dispatch('on_resize', *self.system_size)
File "kivy/_event.pyx", line 695, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:6970)
File "kivy/_event.pyx", line 1168, in kivy._event.EventObservers.dispatch (kivy/_event.c:12154)
File "kivy/_event.pyx", line 1092, in kivy._event.EventObservers._dispatch (kivy/_event.c:11729)
File "/home/onehitfinish/Downloads/Gregulator Files/test.py", line 81, in win_cb
MainScreen().font_size = new_size
File "/home/onehitfinish/Downloads/Gregulator Files/test.py", line 41, in __init__
super(MainScreen, self).__init__(**kwargs)
File "/usr/lib/python3.4/site-packages/kivy/uix/relativelayout.py", line 255, in __init__
super(RelativeLayout, self).__init__(**kw)
File "/usr/lib/python3.4/site-packages/kivy/uix/floatlayout.py", line 66, in __init__
super(FloatLayout, self).__init__(**kwargs)
File "/usr/lib/python3.4/site-packages/kivy/uix/layout.py", line 66, in __init__
super(Layout, self).__init__(**kwargs)
File "/usr/lib/python3.4/site-packages/kivy/uix/widget.py", line 271, in __init__
Builder.apply(self)
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1872, in apply
self._apply_rule(widget, rule, rule)
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 2018, in _apply_rule
e), cause=tb)
kivy.lang.BuilderException: Parser: File "main.kv", line 46:
...
44: text: "Simplify Fraction"
45: pos_hint: {"right": 1, "top":0.2}
>> 46: font_size: root.font_size
47:
48:
...
BuilderException: Parser: File "main.kv", line 46:
...
44: text: "Simplify Fraction"
45: pos_hint: {"right": 1, "top":0.2}
>> 46: font_size: root.font_size
47:
48:
...
AttributeError: 'MainScreen' object has no attribute 'font_size'
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1649, in create_handler
return eval(value, idmap)
File "main.kv", line 46, in <module>
font_size: root.font_size
File "kivy/weakproxy.pyx", line 19, in kivy.weakproxy.WeakProxy.__getattr__ (kivy/weakproxy.c:1101)
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 2011, in _apply_rule
value, rule, rctx['ids'])
File "/usr/lib/python3.4/site-packages/kivy/lang.py", line 1654, in create_handler
cause=tb)
上解決這個,或者暗示這樣做的另一種方式,因爲我很新的Kivy將不勝感激任何幫助。
現在,當我調整窗口的大小,但它似乎並沒有改變MainScreen()它不會破壞或任何FONT_SIZE在win_cb函數中。我不確定MainScreen()是否實際引用了一個全新的實例或某事物,因此將其默認爲25,但是當我的代碼如 def win_cb(window,width,height): new_size = sqrt(width * * 2 +高** 2)40.6 打印(new_size) MainScreen()。FONT_SIZE = new_size 打印(MainScreen()。FONT_SIZE) 它打印像 37.94142906643079 37.94142906643079 任何想法? – Onehitfinish
@Onehitfinish font_size:root.font_size * 80/25似乎有點無意義。似乎其中一個數字應該是一個浮點數,因爲80/25將只是3 – Totem
@Onehitfinish另外,我很確定MainScreen()。font_size = new_size確實創建了一個新的MainScreen實例。唯一的問題是,這個實例甚至沒有被保存到一個變量或任何東西中,所以當win_cb返回時可能會被破壞。另外,爲什麼你在構建方法中定義了win_cb? – Totem