我是wxPython的新手,我使用以下教程创建了几个TextCtrl字段和内容。 http://sebsauvage.net/python/gui/#import
一切正常,除了我不能使用Tab按钮在字段之间切换,这非常烦人。我如何修改该教程中的示例(添加了一些TextCtrl),以便我可以使用tab在字段之间切换?
如果你不想看这个教程,它的基础是使用GridBagSizer将一堆TextCtrl放在它上面。
在网上搜索我发现的所有内容都是“创建一个面板”,但是我试过了,因为我对wxPython很新,所以它没有用,我找不到关于如何做到这一点的全面教程(如果可能的话)我想只坚持一个框架...)
谢谢!
答案 0 :(得分:1)
您需要向框架添加wx.Panel,然后将面板对象作为所有其他窗口小部件的父对象。 wx.Panel添加了Tab键功能,使框架在所有平台上都看起来正确(大多数是正确的颜色)。如果您没有面板,则标签不起作用。
请参阅此主题,其中wxPython的创建者Robin Dunn说同样的话:https://groups.google.com/forum/?fromgroups=#!topic/wxpython-users/gF8r_HwnOEU
答案 1 :(得分: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()