我正在尝试创建一个显示旋转文本的wx.Panel
派生类。但是,当此窗口小部件添加到wx.Sizer
时,文本将不再显示。将其直接添加到wx.Frame
时,文本会显示但会被截断。我做错了什么?
import wx
class Win(wx.Frame):
def __init__(self):
super(Win, self).__init__(None, wx.ID_ANY)
self.p = wx.Panel(self, wx.ID_ANY)
self.p.SetSizer(wx.BoxSizer())
self.rt = RotatedText(self.p, wx.ID_ANY, 'hello', 90)
self.p.GetSizer().Add(self.rt)
class RotatedText(wx.Window):
def __init__(self, parent, id, text, angle, font=None, *args, **kwargs):
super(RotatedText, self).__init__(parent, id, *args, **kwargs)
self.text = text
self.font = font
self.angle = angle
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
def repaint(self, dc):
if self.font:
dc.SetFont(self.font)
text_height, text_width = dc.GetTextExtent(self.text)
dc.SetTextForeground(wx.BLACK)
dc.DrawText(self.text, 0, 0)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.repaint(dc)
event.Skip()
def OnSize(self, event):
dc = wx.ClientDC(self)
self.repaint(dc)
event.Skip()
app = wx.App()
win = Win()
win.Show()
app.MainLoop()
请注意,我使用的是dc.DrawText()
而不是dc.DrawRotatedText()
,因为我试图将问题简化为最简单的形式。最后,我确实希望使用dc.DrawRotatedText()
。
答案 0 :(得分:2)
创建自定义窗口时,请为其指定明确的大小。我在下面列举了一个例子。
发生的事情是因为自定义窗口没有任何与之关联的控件,sizer假设它是空的并且“压缩”它到尽可能小的尺寸(可能是0,0)。
我还添加了绘制旋转文本方法调用和一些任意大小。当然,您可以随意使用它们,直到它满足您的需求。
import wx
class Win(wx.Frame):
def __init__(self):
super(Win, self).__init__(None, wx.ID_ANY)
self.p = wx.Panel(self, wx.ID_ANY)
self.p.SetSizer(wx.BoxSizer())
# set the minimum size when creating the custom window so that the sizer doesn't "squash" it to 0,0
self.rt = RotatedText(self.p, wx.ID_ANY, 'hello', 90,size=(60,60))
self.p.GetSizer().Add(self.rt)
class RotatedText(wx.Window):
def __init__(self, parent, id, text, angle, font=None, *args, **kwargs):
super(RotatedText, self).__init__(parent, id, *args, **kwargs)
self.text = text
self.font = font
self.angle = angle
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_SIZE, self.OnSize)
# set our minimum size based on the text with
dc = wx.MemoryDC()
self.text_width, self.text_height= dc.GetTextExtent(self.text)
# height and width are reversed since we are drawing text vertically
self.SetMinSize((self.text_height,self.text_width))
def repaint(self, dc):
if self.font:
dc.SetFont(self.font)
text_width, text_height = dc.GetTextExtent(self.text)
dc.SetTextForeground(wx.BLACK)
#dc.DrawText(self.text, 0, 0) # change this line to start drawing from bottom to top beginning 60 units from top
dc.DrawRotatedText(self.text, 0, self.text_width, self.angle)
def OnPaint(self, event):
dc = wx.PaintDC(self)
self.repaint(dc)
event.Skip()
def OnSize(self, event):
dc = wx.ClientDC(self)
self.repaint(dc)
event.Skip()
app = wx.App()
win = Win()
win.Show()
app.MainLoop()