wxPython:在现有 wx.Panel 上覆盖 wx.Panel 的好方法 [英] wxPython: Good way to overlay a wx.Panel on an existing wx.Panel

查看:36
本文介绍了wxPython:在现有 wx.Panel 上覆盖 wx.Panel 的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wx.Frame,其中有一个主 wx.Panel,里面有几个小部件.我想要一个按钮来导致帮助面板"出现.这个帮助面板可能是一个 wx.Panel,我希望它覆盖整个主 wx.Panel(不包括 wx.Frame 的菜单栏).帮助按钮上应该有某种关闭按钮,可以让它再次消失.

I have a wx.Frame, in which there is a main wx.Panel with several widgets inside of it. I want one button in there to cause a "help panel" to come up. This help panel would probably be a wx.Panel, and I want it to overlay the entire main wx.Panel (not including the menu bar of the wx.Frame). There should be some sort of close button on the help button that will make it disappear again.

实现这一目标的好方法是什么?我已经研究过 wx.Notebook,但还没有找到让它不显示标签的方法.

What is a good way to achieve this? I've looked into wx.Notebook but haven't found a way to make it not show the tabs.

请注意,我不想在用户每次关闭和打开帮助面板时销毁并重新创建它:我只想将其隐藏.

Note that I don't want to destroy and recreate the help panel every time the user closes and opens it: I just want it to be hidden.

推荐答案

有几种方法

a) 您可以创建一个自定义子面板,并使其在所有子小部件顶部的 0,0 处具有相同的大小和位置.无需销毁它只需显示/隐藏它这也会随着父框架调整大小

a) you can create a custom child panel, and make it same size and position at 0,0 among top of all child widgets. no need of destroying it just Show/Hide it this also resizes with parent frame

b) 弹出一个 wx.PopupWindow 或派生类并将其放置在正确的位置并调整其大小

b) popup a wx.PopupWindow or derived class and place and size it at correct location

如 a) 中的建议,这里是一个示例,其中所有控件都使用 sizer 放置在面板中,因为创建了单独的帮助 cntrl,可以从按钮显示/隐藏,但您可以创建一个自定义的 cntrl,将自身隐藏在点击关闭

so as suggest in a) here is an example, where all controls are put in panel using sizer, as separate help cntrl is created which can be shown/hidden from button, but you can create a custom cntrl which hides itself on clicking close

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)

        self.panel = wx.Panel(self)

        # create controls
        self.cntrlPanel = wx.Panel(self.panel)
        stc1 = wx.StaticText(self.cntrlPanel, label="wow it works")
        stc2 = wx.StaticText(self.cntrlPanel, label="yes it works")
        btn = wx.Button(self.cntrlPanel, label="help?")
        btn.Bind(wx.EVT_BUTTON, self._onShowHelp)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(stc1)
        sizer.Add(stc2)
        sizer.Add(btn)
        self.cntrlPanel.SetSizer(sizer)


        # create help panel
        self.helpPanel = wx.Panel(self.panel)
        self.stcHelp = wx.StaticText(self.helpPanel, label="help help help\n"*8)
        btn = wx.Button(self.helpPanel, label="close[x]")
        btn.Bind(wx.EVT_BUTTON, self._onShowCntrls)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.stcHelp)
        sizer.Add(btn)
        self.helpPanel.SetSizer(sizer)
        self.helpPanel.Hide()
        self.helpPanel.Raise()
        self.helpPanel.SetBackgroundColour((240,250,240))
        self.Bind(wx.EVT_SIZE, self._onSize)

        self._onShowCntrls(None)

    def _onShowHelp(self, event):
        self.helpPanel.SetPosition((0,0))
        self.helpPanel.Show()
        self.cntrlPanel.Hide()

    def _onShowCntrls(self, event):
        self.cntrlPanel.SetPosition((0,0))
        self.helpPanel.Hide()
        self.cntrlPanel.Show()

    def _onSize(self, event):
        event.Skip()
        self.helpPanel.SetSize(self.GetClientSizeTuple())
        self.cntrlPanel.SetSize(self.GetClientSizeTuple())

app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.SetTopWindow(frame)
app.MainLoop()

这篇关于wxPython:在现有 wx.Panel 上覆盖 wx.Panel 的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆