我正在创建一个Python / Kivy应用程序,并且我试图在窗口调整大小时调整文本大小。我已经查了一下,但我还没有找到任何与我期望的完全相同的东西。我想我将on_resize绑定到一个调整文本大小的函数,但是我遇到了很多错误。这是相关的代码。
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
在运行时它运行正常,但是当我调整窗口大小时,我得到了一大堆错误,显然与没有属性font_size的MainScreen相关。
这是错误消息:
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很新,我们将不胜感激。
答案 0 :(得分:0)
尝试在主屏幕中将 font_size 定义为var width = 1840,
height = 1480,
constant = 100,
color = "#BCD8CD"
var nodes = [
{label: '1st stage', x: constant, y: 215 , width:70,height:50 , color :color , stage: true },
{label: '2nd stage', x: constant + 150 , y: 215 ,width:70,height:50 ,color :color, stage: true },
{label: '3rd stage', x: constant + 279, y: 215 ,width:70,height:50, color :color, stage: false },
{label: '4th stage', x: constant + 460, y: 215 ,width:70,height:50, color :color, stage: false },
{label: '5th stage', x: constant + 660, y: 215 ,width:70,height:50 ,color :color, stage: false },
{label: '6th stage', x: constant + 350, y: 350 ,width:70,height:50, color :color, stage: true }
];
var links = [
{ source: 0, target: 1 },
{ source: 1, target: 2},
{ source: 2, target: 3},
{ source: 3, target: 4},
{ source: 1, target: 5}
];
var svg = d3.select('body').append('svg')
.attr('width', width)
.attr('height', height);
var marker = svg.append('marker')
.attr('id',"triangle")
.attr('viewBox',"0 0 10 10")
.attr('refX',"0")
.attr('refY',"5")
.attr('markerUnits','strokeWidth')
.attr('markerWidth','4')
.attr('markerHeight','3')
.attr('orient','auto')
var path = marker.append('path')
.attr('d',"M 0 0 L 10 5 L 0 10 z")
var force = d3.layout.force()
.size([width, height])
.nodes(nodes)
.links(links);
force.linkDistance(width/4);
var link = svg.selectAll('.link')
.data(links)
.enter().append('line')
.attr("stroke-width", "2")
.attr('marker-end','url(#triangle)')
.attr('stroke','black')
var defs = svg.append("defs");
// create filter with id #drop-shadow
// height=130% so that the shadow is not clipped
var filter = defs.append("filter")
.attr("id", "drop-shadow")
.attr("height", "130%");
// SourceAlpha refers to opacity of graphic that this filter will be applied to
// convolve that with a Gaussian with standard deviation 3 and store result
// in blur
filter.append("feGaussianBlur")
.attr("in", "SourceAlpha")
.attr("stdDeviation", 3)
.attr("result", "blur");
// translate output of Gaussian blur to the right and downwards with 2px
// store result in offsetBlur
var feOffset = filter.append("feOffset")
.attr("in", "blur")
.attr("dx", 2)
.attr("dy", 2)
.attr("result", "offsetBlur");
// overlay original SourceGraphic over translated blurred opacity by using
// feMerge filter. Order of specifying inputs is important!
var feMerge = filter.append("feMerge");
feMerge.append("feMergeNode")
.attr("in", "offsetBlur")
feMerge.append("feMergeNode")
.attr("in", "SourceGraphic");
var node = svg.selectAll('.node')
.data(nodes)
.enter().append('g')
.attr('class', 'node')
.attr("transform", function(d){
return "translate("+d.x+","+d.y+")";
})
node.append("rect").attr("class", "nodeRect")
.attr("rx", 6)
.attr("ry", 6)
.attr('width', function(d) { return d.width; })
.attr('height', function(d) { return d.height; })
.style("fill", function(d) { return d.color; })
.transition()
.duration(1000) // this is 1s
.delay(1000)
.style("fill",function(d){if(d.stage) return "#FF9966"})
.style("filter",function(d){if(d.stage) return "url(#drop-shadow)"})
node.append("text").style("text-anchor", "middle")
.style("pointer-events", "none")
.style("font-weight", 900)
.attr("fill", "white")
.style("stroke-width", "0.3px")
.style("font-size", "16px")
.attr("y", function (d){return d.height/2+6;})
.attr("x", function (d){return d.width/2;})
.text(function (d) {return d.label;})
force.start();
link.attr('x1', function(d) { return d.source.x + d.source.width/2; })
.attr('y1', function(d) { return d.source.y + d.source.height/2; })
.attr('x2', function(d) { return d.target.x + d.target.width/2; })
.attr('y2', function(d) { return d.target.y + d.target.height/2; })
.transition()
.duration(1000) // this is 1s
.delay(1000)
.style("filter",function(d){if(d.source.stage) return "url(#drop-shadow)"})
:
NumericProperty