panels showing up

10 views
Skip to first unread message

Nathan smith

unread,
Feb 21, 2021, 8:22:55 PM2/21/21
to wxpytho...@googlegroups.com
Hi folks,


I know I've mentioned this here before and the general agreement seems
to be "It's weird."


I wanted to ask first off, is there any problem with creating a panel
from within the __init__ of the frame?

Like so:


class Mywin(wx.Frame):
 def __init__(self, parent, title):
  super(Mywin, self).__init__(parent, title = title,size = (850,850),
style=wx.DEFAULT_FRAME_STYLE)
  self.SetName("Accessible Calculator.")
  panel = wx.Panel(self, wx.Window.NewControlId(), style= wx.WANTS_CHARS)
  panel.Bind(wx.EVT_KEY_DOWN, self.press)
  self.panel=panel


I'm pretty sure it should work fine, but I just wanted to check. It
doesn't traceback or anything but the panel is not showing up when I run
my showpanel code so I was wondering if there was something backstage
going on crazily there?


Regarding said show panel code, i have it reading as thus:

 def showpanel(self, panel):
  panel.Show()
  panel.Fit()
#  panel.Layout()
#  panel.Update()
  self.Show()
  self.Fit()
  self.Layout()
#  self.Update()
#  self.Refresh()
  self.Center()


The frame does show with the title, but the panel is not showing up,
despite the fact this code works in other programs.


This would suggest there's something wrong with my sizer's?

I have a sample of this shown below:

  vbox2 = wx.BoxSizer(wx.HORIZONTAL)
  vbox = wx.BoxSizer(wx.VERTICAL)
  font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL)
  self.sum = wx.TextCtrl(panel, style=wx.TE_READONLY)
  self.sum.SetFont(font)
  self.sum.Bind(wx.EVT_KEY_DOWN, self.press)
  vbox2.Add(self.sum, 1, wx.EXPAND|wx.ALIGN_LEFT|wx.ALL,5)

# some more widgets being added here to vbox

  vbox2.Add(vbox, 0, wx.EXPAND|wx.ALL, 3)
  vbox2.Add(self.history, 0, wx.EXPAND|wx.ALL, 3) # self.history is a
texctrl that should show up to the right.
  panel.SetSizer(vbox2)
  self.showpanel(panel)


I appreciate any help you can offer and realise it's probably something
really simple not getting through my thick skull.


V Best,

Nathan

Dietmar Schwertberger

unread,
Feb 22, 2021, 1:58:30 PM2/22/21
to wxpytho...@googlegroups.com
Creating a panel in the __init__ method of the frame is fine.
It's difficult to understand from the posted code what kind of layout
you actually want to accomplish, but certainly your structure of init
code, sizers and widgets is overly complicated.
Also, I'm not sure whether EVT_KEY_DOWN for a read only text ctrl will
have any effect.
Then, using EXPAND with a single-line text ctrl in a horizontal sizer is
probably not what you want. It will make grow the text ctrl vertically,
but the number of lines is still 1. Using ALIGN_LEFT in a horizontal
sizer does not have an effect. Maybe it would even trigger an assertion
with more recent versions of wxPython.

Anyway, I have just re-built something similar and this is some
straightforward init code.
(Just ignore the code before self.panel for now or keep your own code
for this.)

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, style=wx.DEFAULT_FRAME_STYLE)
        self.SetSize((850, 850))
        self.SetTitle("Accessible Calculator")
        self.SetName("Accessible Calculator.")

        self.panel = wx.Panel(self, wx.ID_ANY, style=wx.WANTS_CHARS)

        vbox = wx.BoxSizer(wx.VERTICAL)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        vbox.Add(hbox, 1, wx.EXPAND, 0)

        self.sum = wx.TextCtrl(self.panel, wx.ID_ANY, "",
style=wx.TE_READONLY)
        self.sum.SetFont(wx.Font(12, wx.FONTFAMILY_MODERN,
wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
        hbox.Add(self.sum, 0, wx.ALL | wx.EXPAND, 5)

        self.history = wx.TextCtrl(self.panel, wx.ID_ANY, "",
style=wx.TE_READONLY)
        hbox.Add(self.history, 0, wx.EXPAND, 3)

        self.panel.SetSizer(vbox)

        self.Layout()
        self.Centre()

        self.Bind(wx.EVT_KEY_DOWN, self.press, self.panel)
        self.Bind(wx.EVT_KEY_DOWN, self.press, self.sum)

Regards,
Dietmar
AccessibleCalculator.wxg
AccessibleCalculator.py
Reply all
Reply to author
Forward
0 new messages