python wxPython:在现有 wx.Panel 上覆盖 wx.Panel 的好方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1032138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wxPython: Good way to overlay a wx.Panel on an existing wx.Panel
提问by Ram Rachum
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.Frame,其中有一个主 wx.Panel,里面有几个小部件。我想要一个按钮来导致“帮助面板”出现。这个帮助面板可能是一个 wx.Panel,我希望它覆盖整个主 wx.Panel(不包括 wx.Frame 的菜单栏)。帮助按钮上应该有某种关闭按钮,可以让它再次消失。
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.
实现这一目标的好方法是什么?我已经研究过 wx.Notebook 但还没有找到一种方法让它不显示标签。
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.
请注意,我不想在用户每次关闭和打开帮助面板时销毁并重新创建它:我只想隐藏它。
回答by Anurag Uniyal
There are several ways
有几种方式
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
a) 您可以创建一个自定义子面板,并使其大小和位置相同,位于所有子小部件顶部的 0,0 处。无需销毁它,只需显示/隐藏它,这也会随父框架调整大小
b) popup a wx.PopupWindow or derived class and place and size it at correct location
b) 弹出一个 wx.PopupWindow 或派生类并将其放置在正确的位置并调整其大小
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
所以正如 a) 中的建议,这是一个示例,其中所有控件都使用 sizer 放置在面板中,因为创建了单独的帮助 cntrl,可以从按钮显示/隐藏,但您可以创建一个自定义 cntrl,单击关闭时隐藏自身
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()