gpt4 book ai didi

python - wxPython中透明笔记本页面/设置笔记本页面背景图片

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:47 25 4
gpt4 key购买 nike

我正在尝试使用 wxPython 创建一个程序,其中使用选项卡式笔记本。我想将笔记本中页面的背景设置为单个图像。我想我可以将包含笔记本的 wxPanel 的背景设置为图像,然后将页面设置为透明,但我似乎找不到办法。我让背景工作的一种方法是创建 OnEraseBackground 方法(在下面的代码中找到)并将其绑定(bind)到每个页面的 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()

关于python - wxPython中透明笔记本页面/设置笔记本页面背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com