【发布时间】:2018-11-08 18:05:41
【问题描述】:
我一直在尝试最终拥有一个使用简单矢量图像作为背景的应用程序,因此可以根据需要缩放和调整屏幕大小。我暂时用光栅照片对此进行了测试,并试图通过以下链接拼凑一些东西:
https://www.blog.pythonlibrary.org/2010/03/18/wxpython-putting-a-background-image-on-a-panel/ How to resize and draw an image using wxpython? wxPython Background image on frame
不幸的是,我是个菜鸟,虽然我了解了正在发生的事情的要点,但我没有足够的经验来为自己的目的修改它。话虽如此,如何在面板上保留背景图像并使用 wxpython 实时缩放面板大小的图像?
我现在有一些拼凑的代码:
import pathlib
import wx
class MainApp(wx.App):
def __init__(self, redirect=False, filename=None):
wx.App.__init__(self, redirect, filename)
dlg = MainFrame(parent=None,title="IvyVine")
dlg.Show()
####################################
class MainFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent=None, title="IvyVine",size=(1000,500))
panel = MainPanel(self)
self.Center()
self.Show(True)
def OnExit(self,e):
self.Close(True) #Closes the frame
####################################
class MainPanel(wx.Panel):
def __init__(self, parent):
bg_img = 'window.JPG'
wx.Panel.__init__(self, parent=parent)
self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
self.frame = parent
self.bg = wx.Bitmap(bg_img)
self._width, self._height = self.bg.GetSize()
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
for num in range(3):
label = "Button %s" % num
btn = wx.Button(self,label=label)
sizer.Add(btn,0,wx.ALL,5)
hSizer.Add((1,1), 1, wx.EXPAND)
hSizer.Add(sizer, 0, wx.TOP, 100)
hSizer.Add((1,1), 0, wx.ALL, 75)
self.SetSizer(hSizer)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
#---------------------------
def scale_bg(self, bitmap, width, height):
pass
#---------------------------
def OnSize(self, size):
self.Layout()
self.Refresh()
#---------------------------
def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
self.Draw(dc)
#---------------------------
def Draw(self, dc):
cliWidth, cliHeight = self.GetClientSize()
if not cliWidth or not cliHeight:
return
dc.Clear()
# The image I'm using is very large, and this math only captures a part of it for some reason.
xPos = (cliWidth - self._width)/2
yPos = (cliHeight - self._height)/2
#img = self.scale_bg(self.bg)
dc.DrawBitmap(self.bg, xPos, yPos)
#################################
if __name__ == "__main__":
app = MainApp()
app.MainLoop()
****编辑的代码****
####################################
类 MainPanel(wx.Panel):
def __init__(self, parent):
bg_img = 'window.JPG'
wx.Panel.__init__(self, parent=parent)
self.SetBackgroundStyle(wx.BG_STYLE_ERASE)
self.frame = parent
self.bg = wx.Image(bg_img, wx.BITMAP_TYPE_ANY)
#store sizes
self.bgh = self.bg.GetHeight()
self.bgw = self.bg.GetWidth()
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
for num in range(3):
label = "Button %s" % num
btn = wx.Button(self,label=label)
sizer.Add(btn,0,wx.ALL,5)
hSizer.Add((1,1), 1, wx.EXPAND)
hSizer.Add(sizer, 0, wx.TOP, 100)
hSizer.Add((1,1), 0, wx.ALL, 75)
self.SetSizer(hSizer)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_PAINT, self.OnPaint)
#---------------------------
def OnSize(self, size):
self.Layout()
self.Refresh()
#---------------------------
def OnPaint(self, evt):
dc = wx.BufferedPaintDC(self)
self.Draw(dc)
#---------------------------
def Draw(self, dc):
cliWidth, cliHeight = self.GetClientSize()
if not cliWidth or not cliHeight:
return
dc.Clear()
#calculate scale factors
fw = cliWidth / float(self.bgw)
fh = cliHeight / float(self.bgh)
scaledimage = self.bg_img.Scale(fw, fh)
dc.DrawBitmap(wx.Bitmap(scaledimage))
#
【问题讨论】:
标签: python background panel wxwidgets frame