Re: panels showing up

Dietmar Schwertberger <[email protected]> Mon, 22 Feb 2021 19:58:23 +0100
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
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


On 22.02.2021 02:22, Nathan smith wrote:
> 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
>

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/a27c523a-ac6c-71f7-c33d-d04c0444fbb2%40schwertberger.de.
AccessibleCalculator.wxg (text/xml, 2.8 KB)
<?xml version="1.0"?>
<!-- generated by wxGlade 1.0.1 on Mon Feb 22 19:49:39 2021 -->

<application class="MyApp" encoding="UTF-8" for_version="3.0" header_extension=".h" indent_amount="4" indent_symbol="space" is_template="0" language="python" mark_blocks="1" name="app" option="0" overwrite="1" path="AccessibleCalculator.py" source_extension=".cpp" top_window="frame" use_gettext="0" use_new_namespace="1">
    <object class="MyFrame" name="frame" base="EditFrame">
        <extraproperties>
            <property name="Name">"Accessible Calculator."</property>
        </extraproperties>
        <size>850, 850</size>
        <title>Accessible Calculator</title>
        <style>wxDEFAULT_FRAME_STYLE</style>
        <centered>1</centered>
        <object class="wxPanel" name="panel" base="EditPanel">
            <events>
                <handler event="EVT_KEY_DOWN">press</handler>
            </events>
            <style>wxWANTS_CHARS</style>
            <object class="wxBoxSizer" name="vbox" base="EditBoxSizer">
                <orient>wxVERTICAL</orient>
                <object class="sizeritem">
                    <option>1</option>
                    <border>0</border>
                    <flag>wxEXPAND</flag>
                    <object class="wxBoxSizer" name="hbox" base="EditBoxSizer">
                        <orient>wxHORIZONTAL</orient>
                        <object class="sizeritem">
                            <option>0</option>
                            <border>5</border>
                            <flag>wxALL|wxEXPAND</flag>
                            <object class="wxTextCtrl" name="sum" base="EditTextCtrl">
                                <events>
                                    <handler event="EVT_KEY_DOWN">press</handler>
                                </events>
                                <font>
                                    <size>12</size>
                                    <family>modern</family>
                                    <style>normal</style>
                                    <weight>normal</weight>
                                    <underlined>0</underlined>
                                    <face />
                                </font>
                                <style>wxTE_READONLY</style>
                            </object>
                        </object>
                        <object class="sizeritem">
                            <option>0</option>
                            <border>3</border>
                            <flag>wxEXPAND</flag>
                            <object class="wxTextCtrl" name="history" base="EditTextCtrl">
                                <style>wxTE_READONLY</style>
                            </object>
                        </object>
                    </object>
                </object>
            </object>
        </object>
    </object>
</application>
AccessibleCalculator.py (text/plain, 1.8 KB)
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 1.0.1 on Mon Feb 22 19:49:39 2021
#

import wx

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        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)
        # end wxGlade

    def press(self, event):  # wxGlade: MyFrame.<event_handler>
        print("Event handler 'press' not implemented!")
        event.Skip()

# end of class MyFrame

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()