我是wxpython的新手,正在開發我的第一個應用程序。我知道這個話題有很多,但我沒有看到對我的問題特別有幫助。我想有更多的兩個面板,並像這樣開關多個面板之間的WxPython切換
def switch(self, newactpanel)
self.actpanel.Hide()
self.actpanel=newactpanel
self.actpanel.Show()
到目前爲止這是行不通的。該應用程序啓動,按鈕出現,但兩個面板視圖一次出現,或只有一個出現,我不能切換。如何構建一個def來切換更多面板?
非常感謝您的回答。
這裏是我的代碼:
import wx
ID_EXIT=200
ID_ADDKEYWORDS = 301
ID_WRITEINTERP = 302
h_frame = 750
w_frame = 325
class SubPanel(wx.Panel):
""""""
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
class AddKeyWordsPanel(wx.Panel):
def __init__(self, parent,size):
wx.Panel.__init__ (self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = size)
panel1 = SubPanel(self)
some_val = "Add KeyWords"
something = wx.TextCtrl(panel1, -1,some_val, size=(h_frame,24), style=wx.TE_READONLY|wx.SIMPLE_BORDER|wx.TE_CENTRE)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(panel1, 1, wx.EXPAND)
self.SetAutoLayout(True)
self.SetSizer(box)
class WritePanel(wx.Panel):
def __init__(self, parent,size):
wx.Panel.__init__ (self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = size)
panel1 = SubPanel(self)
panel2 = SubPanel(self)
panel3 = SubPanel(self)
keys_val = self.GetKeyNames()
keys = wx.TextCtrl(panel1, -1,keys_val, size=(h_frame,24), style=wx.TE_READONLY|wx.SIMPLE_BORDER|wx.TE_CENTRE)
font = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
keys.SetFont(font)
keywords1_val = self.GetKeyWords(keys_val)
keywords1 = wx.TextCtrl(panel2, -1,keywords1_val, size=(h_frame,100), style=wx.TE_READONLY|wx.BORDER_NONE)
txtentry_val = ""
txtentry = wx.TextCtrl(panel3, -1, txtentry_val, size=(h_frame, 200))
txtentry.SetBackgroundColour("WHITE")
txtentry.SetInsertionPoint(0)
box = wx.BoxSizer(wx.VERTICAL)
box.Add(panel1, 3, wx.EXPAND)
box.Add(panel2, 2, wx.EXPAND)
box.Add(panel3, 1, wx.EXPAND)
self.SetAutoLayout(True)
self.SetSizer(box)
def GetKeyNames(self):
return "Blabla1"
def GetKeyWords(self,keys):
return "Blabla2"
class MainFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title, size=(h_frame, w_frame))
self.CreateStatusBar()
menu= wx.Menu()
menu.Append(ID_ADDKEYWORDS,"&Add KeyWords"," Add Keywords")
menu.Append(ID_WRITEINTERP,"&Write Interpretations"," Write Interpretations")
menu.Append(ID_EXIT,"E&xit"," Terminate the program")
menuBar = wx.MenuBar()
menuBar.Append(menu,"&Menu")
self.SetMenuBar(menuBar)
self.wrtpnl = WritePanel(self,(h_frame,w_frame))
self.addkeywpnl = AddKeyWordsPanel(self,(h_frame,w_frame))
self.panel = self.wrtpnl
self.panel.Layout()
wx.EVT_MENU(self, ID_ADDKEYWORDS, self.SwitchPanel(self.addkeywpnl))
wx.EVT_MENU(self, ID_WRITEINTERP, self.SwitchPanel(self.wrtpnl))
wx.EVT_MENU(self, ID_EXIT, self.OnExit)
self.doiexit = wx.MessageDialog(self, " Exit - R U Sure? \n","GOING away ...", wx.YES_NO)
def SwitchPanel(self,show_pnl):
self.panel.Hide()
self.panel = show_pnl
self.panel.Layout()
self.panel.Show()
self.Layout()
def OnExit(self,e):
igot = self.doiexit.ShowModal()
if igot == wx.ID_YES:
self.Close(True)
app = None
app = wx.App()
frame = MainFrame(None, -1, "KeyWordTool")
frame.Show()
app.MainLoop()
嗨,感謝您的回覆。我知道你的教程,但是我有這個方法的問題。可以說我有20個面板,並希望在它們之間切換。我是否需要編寫一個if self.panel_one,elif self.panel_two ... elif self.panel_twenty?如果可能的話,我正在尋找一種動態,乾淨的好方法。 – Stefan
我只會使用面板字典。然後,當您想要切換時,只需將該鍵傳遞給該面板並切換到該面板即可。或者只是實例化新面板並銷燬舊面板,這樣就不會將所有面板始終放在內存中。 –