2

Pythonプログラムで問題が発生しています。基本的に、それは非常にシンプルなファイルマネージャーです。

私はそれをフォルダ間で移動させようとしています(ユーザーがフォルダをクリックすると、プログラムが表示を更新し、フォルダの内容を表示します)。

私が抱えている問題は、表示を更新するボタンを取得できず、クリックすると新しいフォルダとファイルで埋められないように見えることです。

これが私が使用しているコードで、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)
4

1 に答える 1

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

ところで、wxスタイルは、メソッドとクラスもCamelCasedであることを示していますが、ご存知のとおりです:)

于 2012-10-05T10:37:11.607 に答える