wxpython ListBox未在另一个框架/类中正确更新

时间:2013-07-23 03:22:01

标签: python listbox wxpython

我遇到了一个问题,让我的List控件框将参数从一个类传递到另一个类。我正在使用wxpython创建一个gui,允许用户只从一个目录中选择文件,然后在文本编辑器中打开它。我遇到的问题是虽然它会打开文件,但它不会更新选择。它使其与原始默认选择保持一致。对我所缺少的任何帮助将不胜感激。

正如您所看到的,如果我使用我的机器上的文本编辑器运行它,它知道选择并正常工作。

import wx
import os
import webbrowser

class list(wx.Frame):

        def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'List Directory', size=(600,400))
                panel=wx.Panel(self)

        kk=os.listdir("/Python/Tutorials/Script")

                self.yard=wx.ListBox(panel, -1, (100,50), (400,120), kk, wx.LB_SINGLE)
            self.PcuRestart=wx.Button(panel, 1, 'Restart', pos=(0, 30), size=(60, -1))
        wx.EVT_BUTTON(panel, self.PcuRestart.GetId(), self.pcurestart)

                self.yard.SetSelection(7)

        def pcurestart(self, event):
        MainWindow().Show()
#       self.sel = self.yard.GetSelection()
#       self.k = self.yard.GetString(self.sel)
#       os.environ['probe1'] = self.k
#       print self.k
#               os.system("open /Python/Tutorials/Script/$probe1")

class MainWindow(wx.Frame):
    def __init__(self, filename='boo.txt'):
        super(MainWindow, self).__init__(None, size=(400,200))
    self.q = list(parent=None,id=-1)
        self.q.yard.Refresh()
    self.sel = self.q.yard.GetStringSelection()
    self.q.yard.Refresh()
    self.filename = 'MainProgram' 
    w = str(self.sel)
    print w
    self.dirname = '/Python/Tutorials/Script/'
        self.CreateInteriorWindowComponents()
        textfile = open(os.path.join("/Python/Tutorials/Script", w ), 'r')
        self.control.SetValue(textfile.read())
        textfile.close()


    def CreateInteriorWindowComponents(self):
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)


if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=list(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

非常感谢!

1 个答案:

答案 0 :(得分:0)

这里有一些清理过的代码,显示了一种让它显示多个文本文件的方法:

import wx
import os

class MainWindow(wx.Frame):

    def __init__(self, dirname):
        wx.Frame.__init__(self, None, title='List Directory', size=(600,400))
        panel=wx.Panel(self)
        self.dirname = dirname

        kk=os.listdir(dirname)

        self.yard=wx.ListBox(panel, -1, (100,50), (400,120), kk, wx.LB_SINGLE)
        self.PcuRestart=wx.Button(panel, 1, 'Show File', pos=(0, 30), size=(60, -1))
        self.PcuRestart.Bind(wx.EVT_BUTTON, self.pcurestart)

        self.yard.SetSelection(7)

    def pcurestart(self, event):

        self.sel = self.yard.GetSelection()
        filename = self.yard.GetString(self.sel)
        SubWindow(self.dirname, filename).Show()

class SubWindow(wx.Frame):
    def __init__(self, dirname, filename='boo.txt'):
        super(SubWindow, self).__init__(None, size=(400,200))
        self.CreateInteriorWindowComponents()

        path = os.path.join(dirname, filename)
        print path
        with open(path) as textfile:
            data = textfile.read()

        self.control.SetValue(data)
        textfile.close()

    def CreateInteriorWindowComponents(self):
        self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)


if __name__=='__main__':
    dirname = r'C:\Users\mdriscoll\Downloads'
    app=wx.App(redirect=False)
    frame=MainWindow(dirname)
    frame.Show()
    app.MainLoop()