0

我对 wxPython 很陌生,我使用以下教程创建了几个 TextCtrl 字段和东西。 http://sebsauvage.net/python/gui/#import

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

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

在网上搜索时,我发现的只是“创建一个面板”,但我试过了,因为我对 wxPython 很陌生,所以它没有用,我找不到关于如何做到这一点的详尽教程(如果可能的话,我想只坚持一个框架......)

谢谢!

4

2 回答 2

1

您需要将 wx.Panel 添加到框架,然后将面板对象作为所有其他小部件的父对象。wx.Panel 添加了选项卡功能并使框架在所有平台上看起来都正确(主要是正确的颜色)。如果您没有面板,则选项卡将不起作用。

请参阅 wxPython 的创建者 Robin Dunn 说同样的话的线程:https ://groups.google.com/forum/?fromgroups=#!topic/wxpython-users/gF8r_HwnOEU

于 2013-03-05T14:33:45.713 回答
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()
于 2013-03-04T21:44:39.680 回答