Setting the virtual size's background colour

Hi,

I'm trying to implement something similar to Microsoft paint, where the canvas size may be changed via a dialog box, or by grabbing on the resize "grabbers" on the bottom of the canvas. However, it seems that setting the background colour will paint the entire window.

What I'm doing is asking the user for the width/height of the panel they want to resize to, and setting the virtual size to that. However, it seems it won't go below the panel's size, which I want to keep constant, to have the scrollbars at the bottom and on the side; just for the virtual size to change.

should I just paint a grey background of some sort corresponding to the virtual size, with the paintable area white?

thanks

Sorry, I should have included a sample.

#!/usr/bin/python
import wx
from wx.lib import scrolledpanel as scrolled

class GUI(wx.Frame):
def init(self, parent):
wx.Frame.init(self, parent, size=(1024, 786))
tabs = wx.Notebook(self)
self.board = DrawingPanel(tabs)
tabs.AddPage(self.board, “Tab 1”)

    box = wx.BoxSizer(wx.HORIZONTAL)  # position windows side-by-side
    box.Add(tabs, 2, wx.EXPAND)
    self.SetSizer(box)
    menu = wx.MenuBar()
    _file = wx.Menu()
    _file.Append(wx.ID_NEW, "Resize\tCtrl-R")
    menu.Append(_file, "&File")
    self.SetMenuBar(menu)
    self.Bind(wx.EVT_MENU, self.show, id=wx.ID_NEW)

def show(self, e):
    dlg = Resize(self)
    dlg.ShowModal()
    dlg.Destroy()

class DrawingPanel(wx.ScrolledWindow):
def init(self, tab):
wx.ScrolledWindow.init(self, tab)
self.virtual_size = (1000, 1000)
self.SetVirtualSizeHints(2, 2)
self.SetVirtualSize(self.virtual_size)
self.SetScrollRate(3, 3)

    self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)  # no flicking on Windows!
    self.SetBackgroundColour("White")
    self.ClearBackground()

    self.Bind(wx.EVT_PAINT, self.paint)

def paint(self, event):
    dc = wx.AutoBufferedPaintDC(self)
    dc.Clear()
    dc.DrawText("Thing!", 100, 100)

class Resize(wx.Dialog):
“”"
Allows the user to resize a sheet’s canvas
“”"
def init(self, gui):
“”"
Two text controls for inputting the size, limited to integers only
using a Validator class
“”"
wx.Dialog.init(self, gui, title=“Resize Canvas”)

    self.gui = gui
    gap = wx.LEFT | wx.TOP | wx.RIGHT
    width, height = self.gui.board.virtual_size

    csizer = wx.GridSizer(cols=2, hgap=1, vgap=2)
    self.hctrl = wx.TextCtrl(self)
    self.wctrl = wx.TextCtrl(self)
    csizer.Add(wx.StaticText(self, label="Width:"), 0, wx.TOP | wx.ALIGN_RIGHT, 10)
    csizer.Add(self.wctrl, 1, gap, 7)
    csizer.Add(wx.StaticText(self, label="Height:"), 0, wx.TOP | wx.ALIGN_RIGHT, 7)
    csizer.Add(self.hctrl, 1, gap, 7)

    self.hctrl.SetValue(str(height))
    self.wctrl.SetValue(str(width))

    self.okButton = wx.Button(self, wx.ID_OK, "&OK")
    self.okButton.SetDefault()
    self.cancelButton = wx.Button(self, wx.ID_CANCEL, "&Cancel")

    btnSizer = wx.StdDialogButtonSizer()
    btnSizer.Add(self.okButton, 0, wx.BOTTOM | wx.RIGHT, 5)
    btnSizer.Add(self.cancelButton, 0, wx.BOTTOM | wx.LEFT, 5)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(csizer, 0, gap, 7)
    sizer.Add((10, 10)) # Spacer.
    btnSizer.Realize()
    sizer.Add(btnSizer, 0, gap | wx.ALIGN_CENTRE, 5)
    self.SetSizer(sizer)

    self.okButton.Bind(wx.EVT_BUTTON, self.ok)

def ok(self, event):
    """
    Set the virtual canvas size
    """
    value = (int(self.wctrl.GetValue()), int(self.hctrl.GetValue()))
    board = self.gui.board
    board.virtual_size = value
    board.SetVirtualSize(value)
    #board.SetSize(value)
    board.SetBackgroundColour("Grey")
    board.ClearBackground()
    self.Close()

class TestApp(wx.App):
def OnInit(self):
frame = GUI(None)
frame.Show(True)
return True

if name == ‘main’:
app = TestApp()
app.MainLoop()

Copy of sample.py (3.38 KB)

Steven Sproat wrote:

Hi,

I'm trying to implement something similar to Microsoft paint, where the canvas size may be changed via a dialog box, or by grabbing on the resize "grabbers" on the bottom of the canvas. However, it seems that setting the background colour will paint the entire window.

What I'm doing is asking the user for the width/height of the panel they want to resize to, and setting the virtual size to that. However, it seems it won't go below the panel's size, which I want to keep constant, to have the scrollbars at the bottom and on the side; just for the virtual size to change.

should I just paint a grey background of some sort corresponding to the virtual size, with the paintable area white?

thanks

Did you look at the doodle or pySketch demos in the wxPython "samples" folder? I think they might help you a little. On Windows, the default location is: C:\Program Files\wxPython2.8 Docs and Demos\samples

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Steven Sproat wrote:

Hi,

I'm trying to implement something similar to Microsoft paint, where the canvas size may be changed via a dialog box, or by grabbing on the resize "grabbers" on the bottom of the canvas. However, it seems that setting the background colour will paint the entire window.

What I'm doing is asking the user for the width/height of the panel they want to resize to, and setting the virtual size to that. However, it seems it won't go below the panel's size, which I want to keep constant, to have the scrollbars at the bottom and on the side; just for the virtual size to change.

should I just paint a grey background of some sort corresponding to the virtual size, with the paintable area white?

Probably. The physical area of the window can never be smaller than the client area, no matter what the virtual size is. The full client area will always need to be painted.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!