如何在wxPython的框架中使字段之间的选项卡工作



我是wxPython的新手,我使用下面的教程创建了几个TextCtrl字段和东西。进口http://sebsauvage.net/python/gui/

一切都工作,除了我不能使用Tab按钮切换字段,这是非常烦人的。我如何修改该教程中的示例(添加了更多的TextCtrl),以便我可以使用选项卡在字段之间切换?

如果你不想看教程,它基本上是一个框架,使用GridBagSizer在它上面放了一堆TextCtrl。

在网上搜索时,我发现的是"创建一个面板",但我试过了,因为我是wxPython的新手,它不起作用,我找不到一个关于如何做到这一点的完整教程(如果可能的话,我想坚持只有一个框架…)

谢谢!

您需要添加一个wx。面板到框架,然后将面板对象作为所有其他小部件的父对象。的天气。面板增加了选项卡功能,使框架在所有平台上看起来都是正确的(大多数是正确的颜色)。如果你没有面板,那么选项卡将不起作用。

看看这个线程Robin Dunn, wxPython的创建者,说了同样的话:https://groups.google.com/forum/?fromgroups=#!主题/wxpython-users gF8r_HwnOEU

这是我之前做的一些东西,它不是很好但我可以假设你可以根据需要添加

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

相关内容

  • 没有找到相关文章

最新更新