2012-10-05 3 views
2

저는 파이썬 프로그램에 약간의 문제가있었습니다. 기본적으로 매우 단순한 파일 관리자입니다.버튼을 눌렀을 때 wxpython 새로 고침 창이 나타납니다

나는 폴더 간 이동 (사용자가 폴더를 클릭하면 프로그램이 표시 내용을 새로 고침하고 폴더의 내용을 표시)을 시도하고 있습니다.

내가 겪고있는 문제는 디스플레이를 새로 고치고 클릭 할 때 새 폴더와 파일로 채우는 버튼이있는 것 같습니다.

다음은 현재 사용중인 코드이며 Linux에 있습니다.

import wx 
import fileBrowser 

class interface(wx.Frame): 

    def __init__(self, parent, id): 
     '''(object, int) --> None 
     Set up wx python in a frame and displays it and contents defined in this function on the screen.''' 

     wx.Frame.__init__(self, parent, id, "Bronto", size = (800, 600)) 
     panel = wx.Panel(self) 
     self.createPanels() 
     contents = fileBrowser.print_items("/") 
     wx.StaticText(panel, -1, "/", (50, 10)) 
     col = 50 
     row = 50 
     for items in contents: 
      name  = items 
      col, row = self.makeIcons(panel, (800, 600), name, col, row) 

    def makeIcons(self, panel, param, name, col, row): 
     '''(object, object) --> None 
     Place a button on the window that uses an image as its icon.''' 

     pic = wx.Image("folder.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap() 
     self.button = wx.BitmapButton(panel, -1, pic, pos = (col, row)) 
     wx.StaticText(panel, -1, name, (col + 10, row + 40)) 
     self.Bind(wx.EVT_BUTTON, self.displayContents, self.button) 
     self.button.SetDefault() 

     if(col < 600): 
      return col + 90, row 
     else: 
      col = 50 
      return col, row + 80 


    def createPanels(self): 
     '''(object) --> None 
     Create and place both menu and status bars on the window.''' 

     status  = self.CreateStatusBar() 
     menubar = wx.MenuBar() 
     File  = wx.Menu() 
     Edit  = wx.Menu() 

     menubar.Append(File,"File") 
     menubar.Append(Edit, "Edit") 
     new = wx.MenuItem(File, 101, '&New\tCtrl+N', 'Creates a new document') 
     File.AppendItem(new) 
     self.Bind(wx.EVT_MENU, self.NewApplication, id=101) 
     self.SetMenuBar(menubar) 


    def NewApplication(self, event): 
     app = wx.PySimpleApp() 
     frame = interface(parent = None, id =1) 

     frame.Show() 
     app.MainLoop() 


    def displayContents(self, event): 
     '''(event) --> None 
     Display the contents of the folder clicked on''' 



     #self.panel.Destroy(); 
     #self.panel = wx.Panel(self) 
     self.Refresh(True) 
     contents = fileBrowser.print_items("/home") 
     col = 50 
     row = 50 
     for items in contents: 
      name  = items 
      wx.Yield() 
      col, row = self.makeIcons(panel, (800, 600), name, col, row) 

if __name__ == "__main__": 
    app = wx.PySimpleApp() 
    frame = interface(parent = None, id =1) 

    frame.Show() 
    app.MainLoop() 

그리고 여기에는 fileBrower 프로그램이다 (나는 폴더 만 보는 순간,하지만 난 나중에 변경됩니다)

import os 
import os.path 

def print_items(d): 
    '''(str) -> NoneType 
    Print the list of files and directories in directory d, recursively, 
    prefixing each with indentation.''' 

    icons = [] 
    #print out the names of files and subdirectories 
    for filename in os.listdir(d): 
     subitem = os.path.join(d, filename) 
     if os.path.isdir(subitem): 
      print filename 
      icons.append(filename) 

    return icons 

@pthonm을 : 나는 당신이 제시 한 코드를 추가하지만 것 나던 새로운 것들로 그것을 업데이 트하는 (비록 그것은 창을 지우지)

편집 : 좋아, 나는 거의 일하고있다. self.Refresh (True)를 사용하여 내용을 표시 할 수 있지만 self.panel.Destroy() 메서드를 사용하지 않으면 작동합니다. 그래서, 버튼과 텍스트를 없애는 방법에 대한 제안 (displayContents 메소드가 내가 추가 한 것을 보시오)?

EDIT2 : 제대로 작동합니다. 내가 한 것은 displayContents 메서드에 추가 한 것입니다. 이것은 아마 이런 일을하는 가장 좋은 방법은 아닙니다.

def displayContents(self, event): 
    '''(event) --> None 
    Display the contents of the folder clicked on''' 

    self.panel.Destroy(); 
    self.panel = wx.Panel(self) 
    self.createPanels() 
    self.Update() 
    wx.StaticText(self.panel, -1, location, (50, 10)) 
    contents = fileBrowser.print_items("/home/gum/Documents") 
    col = 50 
    row = 50 
    for directory,name in contents.iteritems(): 
     col, row = self.makeIcons(self.panel, (800, 600), name, col, row) 
+0

당신은이 단순한 파일 관리자와 같이 매우 어렵고 복잡하다고 생각합니다. 거기에 기존의 디자인 패턴과 당신을 위해 더 나은, 규칙에 따라 프로그램. 다음은 코드가있는 예제, 파일 관리자를 코딩하는 방법입니다. "http://zetcode.com/wxpython/skeletons/"이 코드를 연구하고 처음부터 다시 작성하십시오. 내 추천서 ..! – fecub

답변

1

나는 문제는

  • 난 당신이 이전을 저장 제안
  • 이전 패널을 삭제하지 마십시오 각 폴더에 대한 새로운 패널을

    • 을 만드는 것이 생각 패널 어디서나 self.panel에 전화하고 매번 self.panel.Destroy();self.panel = wx.Panel(self)으로 전화하십시오.

      더 나은 옵션은 wx.ListCtrl를 사용 EVT_LIST_ITEM_ACTIVATED를 잡을 다음 목록을 삭제하고 항목을 기입하는 것입니다,

      __init__: 
      self.ListCtrl = wx.ListCtrl(self) 
      self.listCtrl.InsertColumn(0, 'name') 
      self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnChangeFolder, self.listCtrl) 
      self.il = wx.ImageList(16, 16) 
      self.il.Add(wx.Image("folder.png", wx.BITMAP_TYPE_PNG).ConvertToBitmap()) 
      self.listCtrl.AssignImageList(self.il) 
      self.folders = fileBrowser.print_items("/home/gum/Documents") 
      self.UpdateList() 
      UpdateList: 
      self.listCtrl.DeleteAllItems() 
      for index, item in enumerate(self.folders): 
          self.listCtrl.Append((item,)) 
          self.listCtrl.SetItemImage(index, 0) 
          # 0 is the ImageList index, change it for other icons 
      OnChangeFolder: 
      self.folders = file.Browser.print_items(self.listCtrl.GetFocusedItem().GetText()) 
      self.UpdateList 
      

      BTW의 WX 스타일 메소드와 클래스도 낙타 표기법을 나타냅니다 당신이 알아 :

    +0

    displayContents 메소드의 맨 처음에는 self.panel.Destroy() 및 self.panel = wx.Panel (self)를 추가하고 패널을 지 웠습니다.하지만 패널에 새로운 내용을 추가하지는 않겠습니다. – UzSh

    +0

    아마도 문제는 wx.Yield입니까? btw, 질문을 수정해야합니다 – lolopop

    +0

    나는 운 좋게 제거하고 움직여 보았습니다. 어떤 아이디어? 코드를 업데이트했습니다. – UzSh

    관련 문제