How to make wx.statictext text transparent in wxpy

2019-09-17 10:54发布

问题:

# -*- coding: utf-8 -*-
import wx, wx.media
import sqlite3
import os
import time

class MainFream(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.leftPanel = wx.Panel(self, size=(0,0))
        self.media = wx.media.MediaCtrl(self,
                                        #szBackend=wx.media.MEDIABACKEND_GSTREAMER,
                                        szBackend=wx.media.MEDIABACKEND_WMP10,
                                        size = (0,0),
                                        style = wx.SIMPLE_BORDER)
        image = wx.Image('1.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.background = wx.StaticBitmap(self.leftPanel, 0, image, (0,0))


        self.hBoxSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.hBoxSizer.Add(self.leftPanel, 1, wx.ALL|wx.EXPAND, 5)
        self.hBoxSizer.Add(self.media, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(self.hBoxSizer)

        self.conn = sqlite3.connect("test.db")
        self.cursor = self.conn.cursor()

        self.mediaCounter = 0
        self.folderSerach = [f for f in os.listdir() if f.endswith(('.mpg', '.jpg', '.wmv'))]
        self.media.Bind(wx.media.EVT_MEDIA_LOADED, self.mediaPlay, self.media)
        self.media.Bind(wx.media.EVT_MEDIA_FINISHED, self.mediaLoop, self.media)
        self.media.Load(self.folderSerach[self.mediaCounter])

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.autoRefresh, self.timer)
        self.timer.Start(10000)

        self.Text()
        self.LoadText()
        self.TextSizer()

    def autoRefresh(self, event):
        self.LoadText()

    def mediaPlay(self, event):
        if self.folderSerach[self.mediaCounter].endswith('jpg'):
            self.media.Play()
            #time.sleep(10)
        else:
            self.media.Play()

    def mediaLoop(self, evnet):
        self.media.Stop()
        self.mediaCounter += 1
        print(self.folderSerach)
        if self.mediaCounter > len(self.folderSerach) -1: self.mediaCounter = 0
        self.media.Load(self.folderSerach[self.mediaCounter])

    def Text(self):
        self.title = TransparentText(self.leftPanel, label='test title', style=wx.EXPAND)

        self.staticText1_1 = TransparentText(self.leftPanel, label="test1", style=wx.ALIGN_CENTER)
        self.staticText1_2 = TransparentText(self.leftPanel, label="test2", style=wx.ALIGN_CENTER)
        self.staticText1_3 = TransparentText(self.leftPanel, label="test3", style=wx.ALIGN_CENTER)
        self.staticText1_4 = TransparentText(self.leftPanel, label="test4", style=wx.ALIGN_CENTER)
        self.staticText1_5 = TransparentText(self.leftPanel, label="test5", style=wx.ALIGN_CENTER)
        self.staticText1_6 = TransparentText(self.leftPanel, label="test6", style=wx.ALIGN_CENTER)

        self.staticText2_1 = TransparentText(self.leftPanel, style=wx.ALIGN_CENTER)
        self.staticText2_2 = TransparentText(self.leftPanel, style=wx.ALIGN_CENTER)
        self.staticText2_3 = wx.StaticText(self.leftPanel, style=wx.ALIGN_CENTER)
        self.staticText2_4 = wx.StaticText(self.leftPanel, style=wx.ALIGN_CENTER)
        self.staticText2_5 = wx.StaticText(self.leftPanel, style=wx.ALIGN_CENTER)
        self.staticText2_6 = wx.StaticText(self.leftPanel, style=wx.ALIGN_CENTER)

    def LoadText(self):
        self.cursor.execute("SELECT *FROM CLINIC1")
        for data1 in self.cursor.fetchall(): pass
        self.staticText2_1.SetLabel(data1[1])
        self.staticText2_2.SetLabel(data1[2])
        self.staticText2_3.SetLabel(data1[3])
        self.staticText2_4.SetLabel(data1[4])
        self.staticText2_5.SetLabel(data1[5])
        self.staticText2_6.SetLabel(data1[6])

    def TextSizer(self):
        self.gridSizer = wx.GridSizer(3, 6, 0, 5)
        self.textSizers = (
            self.staticText1_1, self.staticText1_2, self.staticText1_3,
            self.staticText1_4, self.staticText1_5, self.staticText1_6,
            self.staticText2_1, self.staticText2_2, self.staticText2_3,
            self.staticText2_4, self.staticText2_5, self.staticText2_6,
        )
        for textSizer in self.textSizers:
            self.gridSizer.Add(textSizer, 0, wx.ALL|wx.ALIGN_CENTER, 1)
        self.vttextSizer = wx.BoxSizer(wx.VERTICAL)
        self.vttextSizer.Add(self.title, 0, wx.ALL|wx.ALIGN_CENTER|wx.TOP, 1)
        self.vttextSizer.Add(self.gridSizer, 1, wx.ALL|wx.ALIGN_CENTER, 1)
        self.leftPanel.SetSizer(self.vttextSizer)

class TransparentText(wx.StaticText):
    def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.TRANSPARENT_WINDOW, name=''):
        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):
        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()

if __name__ == '__main__':
    app = wx.App()
    fream = MainFream()
    fream.Show(True)
    app.MainLoop()

I made the text letters transparent behind the background. The problem is that if you update automatically, the transparent part disappears like the attached picture.

The result is the same using wx.CallLater. Is there a problem with my code? I do not know why. Overlay phenomenon?