按下按钮时wxpython刷新窗口 [英] wxpython refresh window on button press

查看:121
本文介绍了按下按钮时wxpython刷新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python程序遇到了一些麻烦.基本上,它是一个非常非常简单的文件管理器.

I have been having some trouble with my Python program. Basically, it is a very very simple file manager.

我一直试图让它在文件夹之间移动(用户单击文件夹,程序刷新显示并显示文件夹的内容).

I have been trying to get it to move between folders (user clicks a folder, program refreshes display and shows contents of folder).

我遇到的问题是,我似乎无法获得刷新显示的按钮,然后在单击时用新的文件夹和文件填充它.

The problem I am having is that I cant seem to get the button to refresh the display and then fill it with the new folders and files when clicked.

这是我正在使用的代码,它在Linux上.

Here is the code I am using and it is on 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程序(目前我仅查看文件夹,但稍后会进行更改)

And here is the fileBrower program (at the moment I only look at folders, but I will change it later)

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:我添加了您建议的代码,但似乎没有用新的东西对其进行更新(尽管它确实清除了窗口)

@pthonm: I added the code you suggested but it doesnt seem to update it with the new stuff (it does clear the window though)

好的,我几乎可以正常工作了.我可以通过使用self.Refresh(True)来显示内容,但只有在不使用self.panel.Destroy()方法的情况下,它才能起作用.那么,关于如何摆脱按钮和文本的任何建议(有关添加的内容,请参见displayContents方法)?

Okay I almost have it working. I can get it to display the contents by using self.Refresh(True) but it only works if I dont use the self.panel.Destroy() method. So, any suggestions on how to get rid of the buttons and the text(see the displayContents method for what I added)?

我知道了.我所做的是,将其添加到displayContents方法中.不过,这可能不是最好的方法.

I got it to work. What I did was, I added this to my displayContents method. This probably isnt the best way to do this though.

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)每次.

I would suggest you to save the old panel somewhere at self.panel and call self.panel.Destroy();self.panel = wx.Panel(self) on each time.

更好的选择是使用 wx.ListCtrl ,捕获 EVT_LIST_ITEM_ACTIVATED ,然后删除列表并在其中填充项目:

The better option is to use wx.ListCtrl, catch EVT_LIST_ITEM_ACTIVATED and then delete the list and fill it with the items:

__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,只知道:)

BTW, the wx style indicates that methods and classes are also CamelCased, just that you know :)

这篇关于按下按钮时wxpython刷新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆