根据另一个笔记本选项卡中的 TextCtrl() 值更改 wx.ComboBox() 中的选择

发布于 2024-10-30 21:50:07 字数 3303 浏览 5 评论 0原文

我正在使用 wx 制作一个 GUI 来运行另一个程序。我有两个笔记本选项卡:第一个用于输入,第二个用于程序的输出。我想将其设置为用户将在 InputPanel 中指示程序输出的目录,并且 OutputPanel 中的 ComboBox 将使用该目录的内容进行更新。我的代码的精简版本如下所示:

    #!/usr/bin/python

    import glob
    import wx

    class InputPanel(wx.Panel):
        dirF = 0
        def __init__(self, parent):
            wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

            self.dirFileText = wx.StaticText(self, label="Output Directory:")
            self.dirFile = wx.TextCtrl(self, value="fuda", style = wx.TE_PROCESS_ENTER)
            self.Bind(wx.EVT_TEXT, self.EvtText, self.dirFile)

            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.dirFileText, 0, wx.ALL, 2)
            sizer.Add(self.dirFile, 0, wx.ALL|wx.EXPAND,2)
            self.SetSizer(sizer)
            sizer.Fit(self)

        def EvtText(self, event):
            event.GetString()
            InputPanel.dirF = self.dirFile.GetValue()

    class OutputPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

            self.dirText = wx.StaticText(self, label="Choice?")
            self.dirT = wx.ComboBox(self, choices='', style=wx.CB_READONLY)
            self.dirT.SetItems(glob.glob("%s/*.dat" % (InputPanel.dirF)))
            self.Bind(wx.EVT_COMBOBOX, self.onSelect, self.dirT)

            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.dirText, 0, wx.ALL, 2)
            sizer.Add(self.dirT, 0, wx.ALL|wx.EXPAND, 2)
            self.SetSizer(sizer)
            sizer.Fit(self)

        def onSelect(self, event):
            event.GetSelection()

    class Notebook(wx.Notebook):
        def __init__(self, parent):
            wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)

            tabOne = InputPanel(self)
            self.AddPage(tabOne, "Input")
            tabTwo = OutputPanel(self)
            self.AddPage(tabTwo, "Output")

            self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged)
            self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.onPageChanging)

        def onPageChanged(self, event):
            event.Skip()

        def onPageChanging(self, event):
            event.Skip()

class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "nmrPipeFit", size=(600,400))
        panel = wx.Panel(self)

        # Setting up the menu
        filemenu = wx.Menu()
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Exit the program")
        menubar = wx.MenuBar() # Create the menubar
        menubar.Append(filemenu, "&File") # Add file menu to menubar
        self.SetMenuBar(menubar) # Add menubar to frame
        # Set menu events
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        notebook = Notebook(panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()
        self.Show()

    def OnExit(self, event):
        self.Close(True) # Close the frame

if __name__== "__main__":
    app=wx.PySimpleApp()
    frame = MainWindow()
    app.MainLoop()

我尝试了几种不同的方法来获取组合框的选择列表,以通过 InputPanel 中的目录更改进行更新,但没有运气。任何帮助将不胜感激。

提前致谢, 迈克尔·莱瑟姆

I'm using wx to make a GUI for running another program. I have two Notebook tabs: the first for the input and the second for the output of my program. I would like to set it up such that the user will indicate a directory for the program output in the InputPanel, and the ComboBox in the OutputPanel will update with the contents of this directory. A stripped down version of my code looks like:

    #!/usr/bin/python

    import glob
    import wx

    class InputPanel(wx.Panel):
        dirF = 0
        def __init__(self, parent):
            wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

            self.dirFileText = wx.StaticText(self, label="Output Directory:")
            self.dirFile = wx.TextCtrl(self, value="fuda", style = wx.TE_PROCESS_ENTER)
            self.Bind(wx.EVT_TEXT, self.EvtText, self.dirFile)

            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.dirFileText, 0, wx.ALL, 2)
            sizer.Add(self.dirFile, 0, wx.ALL|wx.EXPAND,2)
            self.SetSizer(sizer)
            sizer.Fit(self)

        def EvtText(self, event):
            event.GetString()
            InputPanel.dirF = self.dirFile.GetValue()

    class OutputPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

            self.dirText = wx.StaticText(self, label="Choice?")
            self.dirT = wx.ComboBox(self, choices='', style=wx.CB_READONLY)
            self.dirT.SetItems(glob.glob("%s/*.dat" % (InputPanel.dirF)))
            self.Bind(wx.EVT_COMBOBOX, self.onSelect, self.dirT)

            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.dirText, 0, wx.ALL, 2)
            sizer.Add(self.dirT, 0, wx.ALL|wx.EXPAND, 2)
            self.SetSizer(sizer)
            sizer.Fit(self)

        def onSelect(self, event):
            event.GetSelection()

    class Notebook(wx.Notebook):
        def __init__(self, parent):
            wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)

            tabOne = InputPanel(self)
            self.AddPage(tabOne, "Input")
            tabTwo = OutputPanel(self)
            self.AddPage(tabTwo, "Output")

            self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged)
            self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGING, self.onPageChanging)

        def onPageChanged(self, event):
            event.Skip()

        def onPageChanging(self, event):
            event.Skip()

class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "nmrPipeFit", size=(600,400))
        panel = wx.Panel(self)

        # Setting up the menu
        filemenu = wx.Menu()
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Exit the program")
        menubar = wx.MenuBar() # Create the menubar
        menubar.Append(filemenu, "&File") # Add file menu to menubar
        self.SetMenuBar(menubar) # Add menubar to frame
        # Set menu events
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        notebook = Notebook(panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()
        self.Show()

    def OnExit(self, event):
        self.Close(True) # Close the frame

if __name__== "__main__":
    app=wx.PySimpleApp()
    frame = MainWindow()
    app.MainLoop()

I have tried several different things to get the list of choices for the combobox to update with a change in the directory from the InputPanel, but with no luck. Any help will be greatly appreciated.

Thanks, in advance,
Michael Latham

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鱼忆七猫命九 2024-11-06 21:50:07

好吧,有一种丑陋的黑客方法可以做到这一点,也有一种优雅的方法可以做到这一点。事实上,可能有几个黑客。让我们看一下最常见的一个:

在笔记本的页面更改事件中,您可以告诉输出面板执行如下操作:inputPanelRef.dirFile.GetValue(),然后根据需要更新组合框的条目。您可能需要从笔记本小部件中执行此操作。

我更喜欢在面板之间传递信息的方式是使用 Pubsub。这里有一个很好的例子:
http://www.blog. pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

Well, there's an ugly hack way to do it and the elegant way to do it. Actually, there are probably several hacks. Let's look at one of the most common:

In the notebook's page changing event, you can tell the output panel to do something like this: inputPanelRef.dirFile.GetValue() and then update the comboboxe's entries as needed. You'll probably need to do that from the Notebook widget.

The way I prefer to pass info between panels is to use Pubsub. There's a good example here:
http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

一世旳自豪 2024-11-06 21:50:07

您可以为每个面板添加 getter 和 setter,如下所示:

#!/usr/bin/python

import wx
import os

class InputPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        self.dirFileText = wx.StaticText(self, label="Output Directory:")
        self.dirFile = wx.TextCtrl(self, value=".", style = wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT, self.EvtText, self.dirFile)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.dirFileText, 0, wx.ALL, 2)
        sizer.Add(self.dirFile, 0, wx.ALL|wx.EXPAND,2)
        self.SetSizer(sizer)
        sizer.Fit(self)

        self._dirF = '.'

    def EvtText(self, event):
        event.GetString()
        self._dirF = self.dirFile.GetValue()

    def GetDirF(self):
        return self._dirF

class OutputPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        self.dirText = wx.StaticText(self, label="Choice?")
        self.dirT = wx.ComboBox(self, choices='', style=wx.CB_READONLY)
        self.Bind(wx.EVT_COMBOBOX, self.onSelect, self.dirT)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.dirText, 0, wx.ALL, 2)
        sizer.Add(self.dirT, 0, wx.ALL|wx.EXPAND, 2)
        self.SetSizer(sizer)
        sizer.Fit(self)

    def onSelect(self, event):
        event.GetSelection()

    def setDirT(self, dirT):
        self.dirT.Clear()
        for f in dirT:
            self.dirT.Insert(f, 0)


class Notebook(wx.Notebook):
    def __init__(self, parent):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)

        self.tabOne = InputPanel(self)
        self.AddPage(self.tabOne, "Input")
        self.tabTwo = OutputPanel(self)
        self.AddPage(self.tabTwo, "Output")

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged)

    def onPageChanged(self, event):
        if event.GetSelection() == 1:
            dirf = self.tabOne.GetDirF()
            files = os.listdir(dirf)
            self.tabTwo.setDirT(files)
        event.Skip()

class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "nmrPipeFit", size=(600,400))
        panel = wx.Panel(self)

        # Setting up the menu
        filemenu = wx.Menu()
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Exit the program")
        menubar = wx.MenuBar() # Create the menubar
        menubar.Append(filemenu, "&File") # Add file menu to menubar
        self.SetMenuBar(menubar) # Add menubar to frame
        # Set menu events
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        notebook = Notebook(panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()
        self.Show()

    def OnExit(self, event):
        self.Close(True) # Close the frame

if __name__== "__main__":
    app=wx.PySimpleApp()
    frame = MainWindow()
    app.MainLoop()

您可能想要稍微清理一下,但这说明了如何做到这一点。

You can add a getter and setter for each of the panels like so:

#!/usr/bin/python

import wx
import os

class InputPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        self.dirFileText = wx.StaticText(self, label="Output Directory:")
        self.dirFile = wx.TextCtrl(self, value=".", style = wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT, self.EvtText, self.dirFile)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.dirFileText, 0, wx.ALL, 2)
        sizer.Add(self.dirFile, 0, wx.ALL|wx.EXPAND,2)
        self.SetSizer(sizer)
        sizer.Fit(self)

        self._dirF = '.'

    def EvtText(self, event):
        event.GetString()
        self._dirF = self.dirFile.GetValue()

    def GetDirF(self):
        return self._dirF

class OutputPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)

        self.dirText = wx.StaticText(self, label="Choice?")
        self.dirT = wx.ComboBox(self, choices='', style=wx.CB_READONLY)
        self.Bind(wx.EVT_COMBOBOX, self.onSelect, self.dirT)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.dirText, 0, wx.ALL, 2)
        sizer.Add(self.dirT, 0, wx.ALL|wx.EXPAND, 2)
        self.SetSizer(sizer)
        sizer.Fit(self)

    def onSelect(self, event):
        event.GetSelection()

    def setDirT(self, dirT):
        self.dirT.Clear()
        for f in dirT:
            self.dirT.Insert(f, 0)


class Notebook(wx.Notebook):
    def __init__(self, parent):
        wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)

        self.tabOne = InputPanel(self)
        self.AddPage(self.tabOne, "Input")
        self.tabTwo = OutputPanel(self)
        self.AddPage(self.tabTwo, "Output")

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.onPageChanged)

    def onPageChanged(self, event):
        if event.GetSelection() == 1:
            dirf = self.tabOne.GetDirF()
            files = os.listdir(dirf)
            self.tabTwo.setDirT(files)
        event.Skip()

class MainWindow(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "nmrPipeFit", size=(600,400))
        panel = wx.Panel(self)

        # Setting up the menu
        filemenu = wx.Menu()
        menuExit = filemenu.Append(wx.ID_EXIT, "E&xit", "Exit the program")
        menubar = wx.MenuBar() # Create the menubar
        menubar.Append(filemenu, "&File") # Add file menu to menubar
        self.SetMenuBar(menubar) # Add menubar to frame
        # Set menu events
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

        notebook = Notebook(panel)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
        panel.SetSizer(sizer)
        sizer.Fit(self)
        self.Layout()
        self.Show()

    def OnExit(self, event):
        self.Close(True) # Close the frame

if __name__== "__main__":
    app=wx.PySimpleApp()
    frame = MainWindow()
    app.MainLoop()

You might want to clean this up a bit, but this illustrates how you could do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文