wxpython ListBox未在其他帧/类中正确更新

2024-05-29 06:03:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个问题,让我的列表控制框将参数从一个类传递到另一个类。我使用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()

非常感谢!在


Tags: importselfidinitosdefscriptframe
1条回答
网友
1楼 · 发布于 2024-05-29 06:03:15

下面是一些清理后的代码,展示了一种使其显示多个文本文件的方法:

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

相关问题 更多 >

    热门问题