2013-07-23 18 views
0

我有問題,讓我的列表控件框從一個類傳遞參數到另一個。我正在使用wxpython創建一個GUI,允許用戶只從一個目錄中選擇文件,然後在文本編輯器中打開它。我遇到的問題是,雖然它會打開文件,但不會更新選擇。它保持它與原始默認選擇相同。任何幫助我失蹤將不勝感激。wxpython列表框不能正確更新在另一個框架/類

正如你可以看到,如果我使用我的機器上的文本編輯器運行它,它知道選擇並正常工作。

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

非常感謝!

+0

爲什麼會打開另一幀改變選擇?我可以點擊ListBox中的不同項目而不會出現問題。我真的不明白這個問題。但是,您正在使用一種非常古老的事件綁定方法。你應該這樣做,而不是:self.PcuRestart.Bind(wx.EVT_BUTTON,self.pcurestart)。 PySimpleApp已棄用。你應該使用wx.App(False) –

+0

對不起,我的意思是,儘管你可以在列表框中選擇不同的對象,但當你點擊按鈕時,發生的事情是它總是隻打開第7個選擇,而不是任何一個在列表中選擇一個新的。感覺就像點擊按鈕時,新的選擇永遠不會在MainWindow類中得到確認。我仍在學習wxpython,我將用綁定更新事件,但我不確定wx.App(False)是什麼意思。感謝您的快速響應和幫助!對此,我真的非常感激。 :-) – Chris

+0

wx.PySimpleApp()已棄用。您在代碼的底部使用它。它應該是app = wx.App(redirect = False)。另請參閱我的回答 –

回答

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

非常感謝!這工作完美! :-) – Chris

相關問題