gpt4 book ai didi

python - 使用按钮 wxpython 更改面板

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:36 25 4
gpt4 key购买 nike

import wx
import wx.grid as gridlib

########################################################################
class PanelOne(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
txt = wx.TextCtrl(self)
button =wx.Button(self, label="Save", pos=(200, 325))
button.Bind(wx.EVT_BUTTON, self.onSwitchPanels)

########################################################################
class PanelTwo(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)

grid = gridlib.Grid(self)
grid.CreateGrid(25,12)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(grid, 0, wx.EXPAND)
self.SetSizer(sizer)

########################################################################
class MyForm(wx.Frame):

#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Panel Switcher Tutorial")

self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(self.sizer)

menubar = wx.MenuBar()
fileMenu = wx.Menu()
switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
"Switch Panels",
"Some text")
self.Bind(wx.EVT_MENU, self.onSwitchPanels,
switch_panels_menu_item)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)

#----------------------------------------------------------------------
def onSwitchPanels(self, event):

if self.panel_one.IsShown():
self.SetTitle("Panel Two Showing")
self.panel_one.Hide
self.panel_two.Show()
else:
self.SetTitle("Panel One Showing")
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()


# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

我想在单击 PanelOne 类中的按钮时调用 onSwitchPanels 函数。这个应用程序我想像 this code in Tkinter 一样工作.

我有堆栈人员,帮助我,非常感谢。

最佳答案

一年多前我写了一篇关于这个主题的教程,尽管我使用菜单来进行切换。您可以调整代码以使您的按钮执行此操作。这是教程:http://www.blog.pythonlibrary.org/2010/06/16/wxpython-how-to-switch-between-panels/

编辑:上面代码的问题有三方面。首先在onSwitchPanels方法中,你需要有“self.panel_one.Hide()”。请注意您的代码中缺少的括号。其次,您确实需要取消缩进“self.Layout()”,以便它与 if 语句处于同一级别,而不是在“else”部分内。最后,您不能从 PanelOne 调用“onSwitchPanels”,因为它没有在那里定义。你可以改变它,所以它是这样的:

button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels)

丑陋且不真正推荐,但它有效。不过,您应该为此使用 PubSub。

编辑 #2:我猜我应该刚刚发布代码,因为 OP 甚至不会尝试我的建议。

import wx
import wx.grid as gridlib

########################################################################
class PanelOne(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
txt = wx.TextCtrl(self)
button =wx.Button(self, label="Save", pos=(200, 325))
button.Bind(wx.EVT_BUTTON, parent.onSwitchPanels)

########################################################################
class PanelTwo(wx.Panel):
""""""

#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)

grid = gridlib.Grid(self)
grid.CreateGrid(25,12)

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(grid, 0, wx.EXPAND)
self.SetSizer(sizer)

########################################################################
class MyForm(wx.Frame):

#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY,
"Panel Switcher Tutorial",
size=(800,600))

self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()

self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.panel_one, 1, wx.EXPAND)
self.sizer.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(self.sizer)

menubar = wx.MenuBar()
fileMenu = wx.Menu()
switch_panels_menu_item = fileMenu.Append(wx.ID_ANY,
"Switch Panels",
"Some text")
self.Bind(wx.EVT_MENU, self.onSwitchPanels,
switch_panels_menu_item)
menubar.Append(fileMenu, '&File')
self.SetMenuBar(menubar)

#----------------------------------------------------------------------
def onSwitchPanels(self, event):

if self.panel_one.IsShown():
self.SetTitle("Panel Two Showing")
self.panel_one.Hide()
self.panel_two.Show()
else:
self.SetTitle("Panel One Showing")
self.panel_one.Show()
self.panel_two.Hide()
self.Layout()

# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

关于python - 使用按钮 wxpython 更改面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810514/

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