如何在wxPython的Frame中实现字段之间的Tab切换
我刚接触wxPython,按照一个教程创建了一些文本输入框和其他东西。这个教程的链接是:http://sebsauvage.net/python/gui/#import
一切都正常,除了我无法通过Tab键在这些输入框之间切换,这让我很烦恼。我想知道如何修改这个教程中的例子(我还想加一些文本输入框),这样我就可以用Tab键在输入框之间切换了。
如果你不想看教程,简单来说就是一个框架,上面放了一堆文本输入框,使用了GridBagSizer布局。
我在网上搜索时,只找到“创建一个面板”的建议,但我试过了,由于我对wxPython还很陌生,这个方法没有成功,而且我找不到详细的教程来指导我该怎么做(如果可以的话,我希望只用一个框架...)
谢谢!
2 个回答
0
这是我之前做的一个东西,虽然不是很好,但我相信你可以根据自己的需要进行改进。
import os
import wx
class tab(wx.Panel):
def __init__(self, parent, newid=0, name="New Tab", file=None, aNewTab=False):
wx.Panel.__init__(self, parent)
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
sizer = wx.BoxSizer()
sizer.Add(self.control, -1, wx.EXPAND, newid)
self.SetSizer(sizer)
if file != None:
self.control.write(file)
else:
pass
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(1000,900))
self.CreateStatusBar()
self.buttons = []
filemenu = wx.Menu()
helpmenu = wx.Menu()
menuOpen = filemenu.Append(wx.ID_OPEN, "&Open", "Open a file to edit")
menuSave = filemenu.Append(wx.ID_SAVE, "&Save", "Save the current file")
menuSaveAs = filemenu.Append(wx.ID_SAVEAS, "&Save As", "Save the current file as")
menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Terminate the program")
menuAbout = helpmenu.Append(wx.ID_ABOUT, "&About", "Information about this program,")
menuBar = wx.MenuBar()
menuBar.Append(filemenu, "&File")
menuBar.Append(helpmenu, "&Help")
self.SetMenuBar(menuBar)
self.openFiles = { }
self.p = wx.Panel(self)
self.nb = wx.Notebook(self.p)
self.newTab = tab(self.nb)
self.nb.AddPage(self.newTab, "New Tab")
self.sizer = wx.BoxSizer()
self.sizer.Add(self.nb, 1, wx.EXPAND)
self.p.SetSizer(self.sizer)
#new ids
saveid = wx.NewId()
openid = wx.NewId()
boldid = wx.NewId()
#Set Events
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnSave, menuSave)
self.Bind(wx.EVT_MENU, self.OnSave, menuSaveAs)
# Events that are activated when buttons are pressed
self.Bind(wx.EVT_MENU, self.OnSave, id=saveid)
self.Bind(wx.EVT_MENU, self.OnOpen, id=openid)
self.Bind(wx.EVT_MENU, self.OnBold, id=boldid)
self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('S'), saveid),
(wx.ACCEL_CTRL, ord('O'), openid),
(wx.ACCEL_CTRL, ord('B'), boldid)])
self.SetAcceleratorTable(self.accel_tbl)
self.Show(True)
def OnAbout(self,e):
dlg = wx.MessageDialog(self, "A simple text editor", "About Simple Editor", wx.OK)
dlg.ShowModal()
dlg.Destroy()
def OnExit(self,e):
if self.control.IsModified:
dlg = wx.MessageDialog(self, "If you quit now all your work will be erased. Do you still want to quit?", "Are You Sure?", wx.YES_NO | wx.ICON_QUESTION)
a = dlg.ShowModal()
if a == wx.ID_YES:
self.Close(True)
else:
self.OnSave(self, True)
def OnOpen(self,e):
"""
Open a File
"""
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'r')
newTab = tab(self.nb, name=self.filename, file=f.read(), aNewTab=True)
self.nb.AddPage(newTab, "%s" %(self.filename))
f.close()
self.SetTitle("Simple Editor - %s" %(self.filename))
dlg.Destroy()
def OnSave(self,e, exit=False):
"""
Save a file
"""
#if self.newTab.control.IsEmpty():
#dlg = wx.
self.dirname = ''
dlg = wx.FileDialog(self, "Where do you want to save this file?", self.dirname, "", "*.*", wx.FD_SAVE)
if dlg.ShowModal() == wx.ID_OK:
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname, self.filename), 'w')
a = str(self.control.GetValue())
f.write(a)
f.close()
dlg.Destroy()
if exit != False:
self.Close(True)
self.SetTitle("Simple Editor - %s" %(self.filename))
app = wx.App(False)
frame = MainWindow(None, "Simple Editor")
app.MainLoop()
1
你需要在窗口中添加一个 wx.Panel,然后把这个面板对象作为父级,给其他所有的控件使用。wx.Panel 可以让你使用标签页功能,并且让窗口在不同的平台上看起来更合适(颜色大多是对的)。如果没有这个面板,标签页功能就无法使用。
可以看看这个讨论,wxPython 的创建者 Robin Dunn 也说了同样的事情: https://groups.google.com/forum/?fromgroups=#!topic/wxpython-users/gF8r_HwnOEU