簡體   English   中英

wxPython中筆記本頁面的透明筆記本頁面/設置背景圖像

[英]Transparent Notebook Page/Setting Background Image for Notebook Pages in wxPython

我正在嘗試使用wxPython創建一個程序,其中使用選項卡式筆記本。 我想將筆記本中頁面的背景設置為單個圖像。 我以為可以將包含筆記本的wxPanel的背景設置為圖像,然后將頁面設置為透明,但是我似乎找不到辦法。 使背景工作正常的一種方法是創建方法OnEraseBackground(在下面的代碼中找到)並將其綁定到每個頁面的wx.EVT_ERASE_BACKGROUND。 但是,這樣做意味着每次繪制頁面時,它都會重新繪制同一圖像,這不僅浪費處理能力,而且在重新繪制圖像時也會閃爍,使用時看起來非常糟糕。 我正在尋找有關如何創建透明筆記本頁面的建議,或者,如果這不可能,則防止重新繪制閃光燈。 非常感謝您的寶貴時間。 碼:

""" Imports """
import os
import wx

""" Global Variables """
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath) + "/"
fullscreen = False

"""" Pages for the tab panel """
class Page1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a Page1 object", (20,20))

class Page2(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a Page2 object", (20,20))

class Page3(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a Page3 object", (20,20))

class Page4(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a Page4 object", (20,20))

class Page5(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a Page5 object", (20,20))

""" Main Panel """
class MainPanel(wx.Panel):
    def __init__(self, parent):
        # Create the panel
        wx.Panel.__init__(self, parent)
        nb = wx.Notebook(self, -1)

        # Create the pages
        page1 = Page1(nb)
        page2 = Page2(nb)
        page3 = Page3(nb)
        page4 = Page4(nb)
        page5 = Page5(nb)

        # Add the pages to the notebook
        nb.AddPage(page1, "Page1")
        nb.AddPage(page2, "Page2")
        nb.AddPage(page3, "Page3")
        nb.AddPage(page4, "Page4")
        nb.AddPage(page5, "Page5")

        # Layout Management
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        self.SetSizer(sizer)

        # Add key bindings
        nb.Bind(wx.EVT_KEY_DOWN, self.onKey)
        page1.Bind(wx.EVT_KEY_DOWN, self.onKey)
        page2.Bind(wx.EVT_KEY_DOWN, self.onKey)
        page3.Bind(wx.EVT_KEY_DOWN, self.onKey)
        page4.Bind(wx.EVT_KEY_DOWN, self.onKey)
        page5.Bind(wx.EVT_KEY_DOWN, self.onKey)

        # Add background bindings
        page1.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        page2.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        page3.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        page4.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        page5.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    def OnEraseBackground(self, evt):
        dc = evt.GetDC()
        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap(dname + "background.jpg")
        dc.DrawBitmap(bmp, 0, 0)

    def onKey(self, event):
        key_code = event.GetKeyCode()
        # Toggle FullScreen (F11)
        if key_code == wx.WXK_F11:
            global fullscreen
            fullscreen = not fullscreen
            self.GetParent().ShowFullScreen(fullscreen)
        else:
            event.Skip()

""" Main Frame """
class MainFrame(wx.Frame):
    def __init__(self):
        # Frame and panel creation
        wx.Frame.__init__(self, None, title="Program")
        self.Maximize(True)
        panel = MainPanel(self)

""" Main """
if __name__ == "__main__":
    app = wx.App()
    MainFrame().Show()
    app.MainLoop()

我只創建一個包含背景圖像的面板類。 然后只為您的每個頁面子類化。 像這樣:

import wx

########################################################################
class TabPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    #----------------------------------------------------------------------
    def OnEraseBackground(self, evt):
        """
        Add a picture to the background
        """
        # yanked from ColourDB.py
        dc = evt.GetDC()

        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap("/path/to/image.jpg")
        dc.DrawBitmap(bmp, 0, 0)

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        notebook = wx.Notebook(self)

        tab_one = TabPanel(notebook)
        notebook.AddPage(tab_one, "Tab One")

        tab_two = TabPanel(notebook)
        notebook.AddPage(tab_two, "Tab Two")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)



########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Notebooks")
        panel = MainPanel(self)

        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM