wxPython:使用按钮在多个面板之间切换
我希望有两个(稍后我将添加更多)面板,它们在框架内占据相同的空间,并且在按下工具栏上的相应按钮时显示/隐藏它们,“MLISPANEL”应为默认值。当前,启动应用程序时会显示“设置”面板,按钮不会执行任何操作。我已经搜索和尝试了很多东西好几个小时,但仍然无法让它工作。如果是简单的,我很抱歉,我今天才开始学习python 这就是代码现在的样子:wxPython:使用按钮在多个面板之间切换,python,wxpython,Python,Wxpython,我希望有两个(稍后我将添加更多)面板,它们在框架内占据相同的空间,并且在按下工具栏上的相应按钮时显示/隐藏它们,“MLISPANEL”应为默认值。当前,启动应用程序时会显示“设置”面板,按钮不会执行任何操作。我已经搜索和尝试了很多东西好几个小时,但仍然无法让它工作。如果是简单的,我很抱歉,我今天才开始学习python 这就是代码现在的样子: import wx class mListPanel(wx.Panel): def __init__(self, pare
import wx
class mListPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
#wx.StaticText(self, -1, label='Search:')#, pos=(10, 3))
#wx.TextCtrl(self, pos=(10, 10), size=(250, 50))
class settingsPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
class bifr(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Title")
self.listPanel = mListPanel(self)
self.optPanel = settingsPanel(self)
menuBar = wx.MenuBar()
fileButton = wx.Menu()
importItem = wx.Menu()
fileButton.AppendMenu(wx.ID_ADD, 'Add M', importItem)
importItem.Append(wx.ID_ANY, 'Import from computer')
importItem.Append(wx.ID_ANY, 'Import from the internet')
exitItem = fileButton.Append(wx.ID_EXIT, 'Exit')
menuBar.Append(fileButton, 'File')
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.Quit, exitItem)
toolBar = self.CreateToolBar()
homeToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'Home', wx.Bitmap('icons/home_icon&32.png'))
importLocalToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'Import from computer', wx.Bitmap('icons/comp_icon&32.png'))
importToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'Import from the internet', wx.Bitmap('icons/arrow_bottom_icon&32.png'))
settingsToolButton = toolBar.AddLabelTool(wx.ID_ANY, 'settings', wx.Bitmap('icons/wrench_plus_2_icon&32.png'))
toolBar.Realize()
self.Bind(wx.EVT_TOOL, self.switchPanels(), settingsToolButton)
self.Bind(wx.EVT_TOOL, self.switchPanels(), homeToolButton)
self.Layout()
def switchPanels(self):
if self.optPanel.IsShown():
self.optPanel.Hide()
self.listPanel.Show()
self.SetTitle("Home")
elif self.listPanel.IsShown():
self.listPanel.Hide()
self.optPanel.Show()
self.SetTitle("Settings")
else:
self.SetTitle("Error")
self.Layout()
def Quit(self, e):
self.Close()
if __name__ == "__main__":
app = wx.App(False)
frame = bifr()
frame.Show()
app.MainLoop()
首先,我强烈建议您在深入研究wxpython之前,尽快了解并充分理解它们(它们其实并不难理解),这只是一个友好的提示:) 关于你的例子,有几件事: 当您不使用sizers时,您必须为每个窗口指定大小和位置,否则它们将无法显示,因此您必须将面板类更改为类似的内容(同样,这只是为了演示,您应该使用wx.sizers,而不是位置和大小): 此外,在绑定事件时,它应该如下所示:
self.Bind(wx.EVT_TOOL, self.switchPanels, settingsToolButton)
self.Bind(wx.EVT_TOOL, self.switchPanels, homeToolButton)
def switchPanels(self, event):
if self.optPanel.IsShown():
self.optPanel.Hide()
self.listPanel.Show()
self.SetTitle("Home")
elif self.listPanel.IsShown():
self.listPanel.Hide()
self.optPanel.Show()
self.SetTitle("Settings")
else:
self.SetTitle("Error")
self.Layout()
注意,我是如何编写函数名称而不添加()的,因为事件传递给它,您不能将自己的参数输入到事件发出的函数(除非使用以下语法lambda e:FooEventHandler(paramaters))进行此操作)
事件处理程序(函数)应如下所示:
self.Bind(wx.EVT_TOOL, self.switchPanels, settingsToolButton)
self.Bind(wx.EVT_TOOL, self.switchPanels, homeToolButton)
def switchPanels(self, event):
if self.optPanel.IsShown():
self.optPanel.Hide()
self.listPanel.Show()
self.SetTitle("Home")
elif self.listPanel.IsShown():
self.listPanel.Hide()
self.optPanel.Show()
self.SetTitle("Settings")
else:
self.SetTitle("Error")
self.Layout()
当事件对象传递到事件中时,绑定到事件的函数中的self旁边应该始终有第二个参数,您可以在文档中找到它的关联方法和参数(在本例中是wx.EVT_工具)