wxPython - change panel by button

青春壹個敷衍的年華 提交于 2020-07-08 02:37:27

问题


I would like to write simple code with 2 panels. Simple "Hello" and button next to it. Button changes panel to panel_2 with text "Panel 2" and button next to it. I do not know how can I achieve that. There is my code:

import wx


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

        self.SetSize((800,600))

        tekst = 'HELLO'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)

        btn = wx.Button(self, -1, "Change panel", (345, 100))
        self.Bind(wx.EVT_BUTTON, Program.zmiana, btn)

 class Glowne1(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        tekst = 'Panel 2'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
    wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)

        btn = wx.Button(self, -1, "Change panel", (345, 100))
        #self.Bind(wx.EVT_BUTTON, frame.zmiana, btn)



class Program(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,'Program')

        panel_one = Glowne(self)
        self.panel_two = Glowne1(self)
        self.panel_two.Hide()
        self.SetSize((800,600))
        self.Centre()
    def zmiana(self):
        self.panel_one.Hide()
        self.panel_two.Show()   
if __name__ == "__main__":
    app = wx.App(False)
    frame = Program()
    frame.Show()
    app.MainLoop()

Thank you for any help. I read this tutorial but I can't make it work in my simple code.


回答1:


Program Frame needs a sizer added, the panels need self added to btn so they can be accesed from class Program, bind to the buttons inside of the class Program.

Here is an example

import wx


class Glowne(wx.Panel):

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

        self.SetSize((800, 600))

        tekst = 'HELLO'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)

        self.btn = wx.Button(self, -1, "Change panel", (345, 100))


class Glowne1(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        tekst = 'Panel 2'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)

        self.btn = wx.Button(self, -1, "Change panel", (345, 100))


class Program(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, 'Program')

        sizer = wx.BoxSizer()
        self.SetSizer(sizer)

        self.panel_one = Glowne(self)
        sizer.Add(self.panel_one, 1, wx.EXPAND)
        self.panel_one.btn.Bind(wx.EVT_BUTTON, self.show_panel_two)
        self.panel_two = Glowne1(self)
        sizer.Add(self.panel_two, 1, wx.EXPAND)
        self.panel_two.btn.Bind(wx.EVT_BUTTON, self.show_panel_one)
        self.panel_two.Hide()
        self.SetSize((800, 600))
        self.Centre()

    def show_panel_one(self, event):
        self.panel_one.Show()
        self.panel_two.Hide()
        self.Layout()

    def show_panel_two(self, event):
        self.panel_two.Show()
        self.panel_one.Hide()
        self.Layout()


if __name__ == "__main__":
    app = wx.App(False)
    frame = Program()
    frame.Show()
    app.MainLoop()


来源:https://stackoverflow.com/questions/36399627/wxpython-change-panel-by-button

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