Wxpython panel is cropped with only a small box shown at the top left hand corner

别来无恙 提交于 2021-01-29 18:58:39

问题


I am using Hide() and Show() from wx to do the "next page" effect by hiding a panel and showing the next one but in the same frame (not very sure if I am doing it correctly though). At certain pages, the panel is just a small cropped version at the top left corner while some other panels can work normally (display the full thing). How do I solve this problem?

I saw something on stackoverflow about child/parent of the panel or frame and tried changing my code but it does not work, not very sure about how to do it correctly.

class MyPanel(wx.Panel):
    def __init__(self, parent):
        #Constructor
        wx.Panel.__init__(self, parent=parent)
        #self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        #This is for older versions of wx
        self.frame = parent
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Layout()

    def OnEraseBackground(self, evt):
        #Add background pic
        #From ColourDB.py
        dc = evt.GetDC()

        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap("RszHive.jpg")
        dc.DrawBitmap(bmp, 0, 0)

class StartPage(wx.Frame):
    def __init__(self, current_dt):
        #Settings for frame
        super().__init__(parent=None, title='Test', size=(850,790))
        #setting up main panel (home page)
        self.panel = MyPanel(self)
        self.current_dt = current_dt
        #so that frame will be in the center of the screen
        self.Center()

        self.vert_sizer = wx.BoxSizer(wx.VERTICAL)

        from Database import DataBase, OperatingHours, GetDayTime, GetMenuByDayTime

        dDataBase = DataBase("Full_Menu_Database.txt")
        dOperatingHours = OperatingHours("Operating Hours.txt")

        # Convert to a tuple needed for the functions
        tDayTime = GetDayTime(self.get_dt())

        # Function to get menu dictionary by date and time
        # Will return an empty dictionary if no food/stores are available
        self.stores_open = GetMenuByDayTime(dDataBase, dOperatingHours, tDayTime)

        if self.stores_open == {}:
            self.ophours = wx.StaticText(self.panel, -1, style=wx.ALIGN_CENTER)
            self.ophours.SetLabel("Test")
            self.ophours_font = wx.Font(19, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
            self.ophours.SetFont(self.ophours_font)
            self.vert_sizer.Add(self.ophours, 0, wx.ALL | wx.CENTER, 10)

        else:
            self.store_names, self.stores_ = [], []
            for onestorename in self.stores_open.keys():
                self.store_names.append(onestorename)
                self.stores_.append(self.stores_open[onestorename])

            #btn for store1
            store_btn1 = wx.Button(self.panel, label= self.store_names[0])
            store_btn1.Bind(wx.EVT_BUTTON, self.click_store1)
            self.vert_sizer.Add(store_btn1, 0, wx.ALL | wx.CENTER, 5)

            #btn for store2 if have
            if len(self.store_names) > 1:
                store_btn2 = wx.Button(self.panel, label=self.store_names[1])
                store_btn2.Bind(wx.EVT_BUTTON, self.click_store2)
                self.vert_sizer.Add(store_btn2, 0, wx.ALL | wx.CENTER, 5)

            # btn for store3 if have
            if len(self.store_names) > 2:
                store_btn3 = wx.Button(self.panel, label=self.store_names[2])
                store_btn3.Bind(wx.EVT_BUTTON, self.click_store3)
                self.vert_sizer.Add(store_btn3, 0, wx.ALL | wx.CENTER, 5)

            # btn for store4 if have
            if len(self.store_names) > 3:
                store_btn4 = wx.Button(self.panel, label=self.store_names[3])
                store_btn4.Bind(wx.EVT_BUTTON, self.click_store4)
                self.vert_sizer.Add(store_btn4, 0, wx.ALL | wx.CENTER, 5)

            # btn for store5 if have
            if len(self.store_names) > 4:
                store_btn5 = wx.Button(self.panel, label=self.store_names[4])
                store_btn5.Bind(wx.EVT_BUTTON, self.click_store5)
                self.vert_sizer.Add(store_btn5, 0, wx.ALL | wx.CENTER, 5)

        self.SetSizer(self.vert_sizer)
        self.Layout()
        self.Show()

Picture of what the panel looks like when i run the code


回答1:


@igor is correct a call to Layout will get the job done.
Here is an example: Click on the displayed panel to swap to the other one.

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 1", size=(250,75))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()

    def switch(self, event):
        self.parent.Swap()

class MyOtherPanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 2", size=(175,250))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()
        self.panel.Hide()

    def switch(self, event):
        self.parent.Swap()

class PanelSwitcher(wx.Frame):
    def __init__(self):
        super().__init__(None)

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.panel1 = MyPanel(self)
        self.panel2 = MyOtherPanel(self)
        vbox.Add(self.panel1)
        vbox.Add(self.panel2)
        self.SetSizer(vbox)
        self.Show()

    def Swap(self):
        if self.panel1.panel.IsShown():
            self.panel1.panel.Hide()
            self.panel2.panel.Show()
        else:
            self.panel2.panel.Hide()
            self.panel1.panel.Show()
        self.Layout()


if __name__ == "__main__":
    app = wx.App()
    PanelSwitcher()
    app.MainLoop()


来源:https://stackoverflow.com/questions/58794753/wxpython-panel-is-cropped-with-only-a-small-box-shown-at-the-top-left-hand-corne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!