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()
非常感謝!
爲什麼會打開另一幀改變選擇?我可以點擊ListBox中的不同項目而不會出現問題。我真的不明白這個問題。但是,您正在使用一種非常古老的事件綁定方法。你應該這樣做,而不是:self.PcuRestart.Bind(wx.EVT_BUTTON,self.pcurestart)。 PySimpleApp已棄用。你應該使用wx.App(False) –
對不起,我的意思是,儘管你可以在列表框中選擇不同的對象,但當你點擊按鈕時,發生的事情是它總是隻打開第7個選擇,而不是任何一個在列表中選擇一個新的。感覺就像點擊按鈕時,新的選擇永遠不會在MainWindow類中得到確認。我仍在學習wxpython,我將用綁定更新事件,但我不確定wx.App(False)是什麼意思。感謝您的快速響應和幫助!對此,我真的非常感激。 :-) – Chris
wx.PySimpleApp()已棄用。您在代碼的底部使用它。它應該是app = wx.App(redirect = False)。另請參閱我的回答 –