在下面的玩具示例中,我在可调整大小的窗口中有一个文本布局。当窗口重新调整大小时,文本对于某些分辨率会变得模糊(尝试一下)。这是为什么?
import pyglet
width, height = 500, 500
window = pyglet.window.Window(width, height, resizable=True)
batch = pyglet.graphics.Batch()
document = pyglet.text.document.FormattedDocument()
document.insert_text(0, 'Hello world!', attributes=dict(font_name='Arial', font_size=12, color=(255, 255, 255, 255)))
layout = pyglet.text.layout.IncrementalTextLayout(document, width, height, multiline=True, batch=batch)
# for drawing graphics
pyglet.gl.glLineWidth(3)
outline = batch.add(4, pyglet.gl.GL_LINE_LOOP, None, ('v2f', (0, 0, width, 0, width, height, 0, height)), ('c4B', (255, 0, 0, 0)*4))
@window.event
def on_resize(width, height):
document.delete_text(0, len(document.text))
document.insert_text(0, "Layout has been resized to {}x{}".format(0.9*width, 0.9*height))
layout.width = 0.9*width
layout.height = 0.9*height
layout.x = (width - layout.width)*0.5
layout.y = (height - layout.height)*0.5
outline.vertices = (layout.x, layout.y, layout.width + layout.x, layout.y, layout.width + layout.x, layout.height + layout.y, layout.x, layout.height + layout.y)
@window.event
def on_draw():
window.clear()
batch.draw()
pyglet.app.run()
答案 0 :(得分:1)
事情开始变得不稳定,因为布局被赋予十进制宽度和高度。修复删除模糊文本问题:
import pyglet
width, height = 500, 500
window = pyglet.window.Window(width, height, resizable=True)
batch = pyglet.graphics.Batch()
document = pyglet.text.document.FormattedDocument()
document.insert_text(0, 'Hello world!', attributes=dict(font_name='Arial', font_size=12, color=(255, 255, 255, 255)))
layout = pyglet.text.layout.IncrementalTextLayout(document, width, height, multiline=True, batch=batch)
# for drawing graphics
pyglet.gl.glLineWidth(3)
outline = batch.add(4, pyglet.gl.GL_LINE_LOOP, None, ('v2f', (0, 0, width, 0, width, height, 0, height)), ('c4B', (255, 0, 0, 0)*4))
@window.event
def on_resize(width, height):
width, height = int(width), int(height)
window.set_size(width, height)
layout_width, layout_height = int(0.9*width), int(0.9*height)
document.delete_text(0, len(document.text))
document.insert_text(0, "Layout has been resized to {}x{}".format(layout_width, layout_height))
layout.width, layout.height = layout_width, layout_height
layout.x = int((width - layout_width)*0.5)
layout.y = int((height - layout_height)*0.5)
outline.vertices = (layout.x, layout.y, layout.width + layout.x, layout.y, layout.width + layout.x, layout.height + layout.y, layout.x, layout.height + layout.y)
@window.event
def on_draw():
window.clear()
batch.draw()
pyglet.app.run()
答案 1 :(得分:0)
我遇到了同样的问题,我让它像这样工作:
您将需要一个包含on_resize事件处理程序的包装器类。它还需要父窗口。
class Label(pyglet.text.Label):
def __init__(self, window,**kwargs):
self.window = window
self.window.push_handlers(self)
self.init_settings = dict()
for k,v in kwargs.items():
if callable(v):
kwargs[k] = v(self.window)
self.init_settings[k] = v
super().__init__(**kwargs)
def on_resize(self, width, height):
for k,v in self.init_settings.items():
setattr(self,k,v(self.window))
使用方法: 在调整窗口大小时需要更新的每个属性都是lambda函数。在这里,您应该使用初始窗口大小编写标签的初始值。
class WindowSubclass(pyglet.window.Window):
def __init__(self,*args,**kwargs):
self.initial_size = self.width,self.height
self.label = Label(window=self,
text='PAUSED',
multiline=False,
font_name='Times New Roman',
bold=True,
font_size=lambda win:24*win.height/win.initial_height,
width=lambda win:win.width/4,
x=lambda win:win.width/2,
y=lambda win:win.height/2,
color=(255, 255, 255, 255),
anchor_x='center', anchor_y='center')