按钮上的wxpython刷新窗口按
问题描述:
我一直在使用我的Python程序时遇到了一些麻烦。基本上,它是一个非常非常简单的文件管理器。按钮上的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方法)?
编辑2:我得到它的工作。我所做的是,我将其添加到我的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)
答
我认为问题是,你
- 为每个文件夹
- 不要删除旧板
我建议你保存旧创建新的面板面板在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风格表明,方法和类也驼峰格式,只是你知道:)
你想这么简单的文件管理器很困难和复杂。现有的设计模式及其对您更好,根据规则进行编程。这里是一个代码示例,如何编写一个文件管理器。 “http://zetcode.com/wxpython/skeletons/”研究这段代码并从头开始写。我对你的建议..! – fecub