gpt4 book ai didi

python - wxPython:使用按钮在多个面板之间切换

转载 作者:太空宇宙 更新时间:2023-11-03 16:37:54 26 4
gpt4 key购买 nike

我希望有两个(稍后我将添加更多)面板在框架内占据相同的空间,并在按下工具栏上的相应按钮时显示/隐藏它们,“mListPanel”应该是默认值。目前,设置面板在应用程序启动时显示,并且按钮不执行任何操作。我已经搜索并尝试了很多东西几个小时,但仍然无法让它工作。如果这很简单,我很抱歉,我今天才开始学习 python。

这就是代码现在的样子:

    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并在深入研究 wxpython 之前尽快充分理解它们(它们实际上并不难理解),只是一个友好的提示:)。

就您的示例而言,有以下几点:当您不使用大小调整器时,您必须为每个窗口指定大小和位置,否则它们将不会显示,因此您必须将面板类更改为类似这样的内容(同样,这仅用于演示,您应该使用 wx.sizers 执行此操作,而不是位置和大小):

class mListPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent,pos=(0,100),size=(500,500))

class settingsPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent,pos=(0,200),size (1000,1000))

此外,绑定(bind)事件时它应该如下所示:

self.Bind(wx.EVT_TOOL, self.switchPanels, settingsToolButton)
self.Bind(wx.EVT_TOOL, self.switchPanels, homeToolButton)

请注意,我如何只编写函数的名称,而没有添加 (),当事件传递给它时,您无法将自己的参数输入到从事件发出的函数中(除非您使用以下命令来执行此操作)语法 lambda e:FooEventHandler(paramaters))

事件处理程序(函数)应如下所示:

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()

在绑定(bind)到事件的函数中,当事件对象传递到那里时,在 self 旁边应该始终有第二个参数,并且您可以在文档中找到其关联的方法和参数(在本例中,它是 wx.EVT_TOOL )。

关于python - wxPython:使用按钮在多个面板之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37061280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com