1 year ago
#379499

Ruthless
How to add smaller panel on top of existing panel inside frame? - WXPYTHON
Below I have an example of my code, you can drag and drop an image from your desktop into the window and it will change the image in the app. I want to only drag/drop images into the image, not the border around the image in the window.
In another words, you can drag/drop images anywhere in the window, but I only want you to be able to drag/drop images on top of the image.
import wx
from wx import *
import wx.lib.statbmp as SB
from PIL import Image
from pubsub import pub
import wx.lib.inspection #inspection tool
#=======================================================================#
# DRAG/DROP
#=======================================================================#
PhotoMaxSize = 485
class DropTarget(wx.FileDropTarget):
def __init__(self, widget):
wx.FileDropTarget.__init__(self)
self.widget = widget
def OnDropFiles(self, x, y, filenames):
pub.sendMessage('dnd', filepath=filenames[0])
return True
#=======================================================================#
# FRAME
#=======================================================================#
class PhotoCtrl(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Learning GUI', size=(800,500), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
self.SetBackgroundColour('#1a1a1a')
self.panel = MainPanel(self)
self.main_sizer = wx.BoxSizer()
self.main_sizer.Add(self.panel, 0, wx.EXPAND, 10)
self.Show()
#=======================================================================#
# DRAG/DROP IMAGE PANEL
#=======================================================================#
class MainPanel(wx.Panel):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
bg3 = wx.Image('bg3.jpg', wx.BITMAP_TYPE_ANY)
img = wx.Image(bg3)
self.image_ctrl = SB.GenStaticBitmap(
self, wx.ID_ANY, wx.Bitmap(img))
file_drop_target = DropTarget(self)
self.SetDropTarget(file_drop_target)
pub.subscribe(self.update_image_on_dnd, 'dnd')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.AddStretchSpacer(5)
sizer.Add(self.image_ctrl, 0, wx.ALIGN_CENTER, 10)
sizer.AddStretchSpacer(1)
self.SetSizer(sizer)
def update_image_on_dnd(self, filepath):
self.on_view(filepath=filepath)
def on_view(self, filepath):
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
W = img.GetWidth()
H = img.GetHeight()
if W > H:
new_w = PhotoMaxSize
new_h = PhotoMaxSize * H / W
else:
new_h = PhotoMaxSize
new_w = PhotoMaxSize * W / H
img = img.Scale(new_w, new_h)
self.image_ctrl.SetBitmap(wx.Bitmap(img))
#self.Fit() #removing this makes window size not change when dropping pic in
self.parent.Fit()
#=======================================================================#
# END
#=======================================================================#
if __name__ == '__main__':
app = wx.App()
frame = PhotoCtrl()
wx.lib.inspection.InspectionTool().Show() #inspection tool
app.MainLoop()
python
python-3.x
wxpython
0 Answers
Your Answer