WXPython Python 3.10
我想要为我的窗口添加背景(在网上找了代码),但开始挺成功,但我想要显示文本时,背景不见了.
控制台没有报错
我觉得他应该没有错误
import wx
class StartFrame(wx.Frame):
def __init__(self, parent=None, ID=-1):
super().__init__(parent, title="ServerNet v0.1 By Jemods.",
size=(1440, 900), pos=(100, 20), id=ID)
self.panel = wx.Panel(self)
self.panel.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBack)
font = wx.Font(30, wx.DEFAULT, wx.NORMAL, wx.BOLD, faceName="Minecraft AE")
text = TransparentText(self, -1 , "hi", style=wx.ALIGN_CENTER)
text.SetForegroundColour('blue')
text.SetFont(font)
def OnEraseBack(self, event):
dc = event.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("resources/back.jpg")
dc.DrawBitmap(bmp, 0, 0)
class TransparentText(wx.StaticText):#继承了wx.Statictext的类,并对相应的方法进行重写;
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TRANSPARENT_WINDOW, name='transparenttext'):
wx.StaticText.__init__(self, parent, id, label, pos, size, style, name)
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
self.Bind(wx.EVT_SIZE, self.on_size)
def on_paint(self, event):#重写on_paint可以对控件进行重写重新构造形状
bdc = wx.PaintDC(self)
dc = wx.GCDC(bdc)
font_face = self.GetFont()
font_color = self.GetForegroundColour()
dc.SetFont(font_face)
dc.SetTextForeground(font_color)
dc.DrawText(self.GetLabel(), 0, 0)
def on_size(self, event):
self.Refresh()
event.Skip()
def main():
app= wx.App()
frame = StartFrame()
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()