Example TreeItem subclass -- browse the file system.
| 392 | # Example application |
| 393 | |
| 394 | class FileTreeItem(TreeItem): |
| 395 | |
| 396 | """Example TreeItem subclass -- browse the file system.""" |
| 397 | |
| 398 | def __init__(self, path): |
| 399 | self.path = path |
| 400 | |
| 401 | def GetText(self): |
| 402 | return os.path.basename(self.path) or self.path |
| 403 | |
| 404 | def IsEditable(self): |
| 405 | return os.path.basename(self.path) != "" |
| 406 | |
| 407 | def SetText(self, text): |
| 408 | newpath = os.path.dirname(self.path) |
| 409 | newpath = os.path.join(newpath, text) |
| 410 | if os.path.dirname(newpath) != os.path.dirname(self.path): |
| 411 | return |
| 412 | try: |
| 413 | os.rename(self.path, newpath) |
| 414 | self.path = newpath |
| 415 | except OSError: |
| 416 | pass |
| 417 | |
| 418 | def GetIconName(self): |
| 419 | if not self.IsExpandable(): |
| 420 | return "python" # XXX wish there was a "file" icon |
| 421 | |
| 422 | def IsExpandable(self): |
| 423 | return os.path.isdir(self.path) |
| 424 | |
| 425 | def GetSubList(self): |
| 426 | try: |
| 427 | names = os.listdir(self.path) |
| 428 | except OSError: |
| 429 | return [] |
| 430 | names.sort(key = os.path.normcase) |
| 431 | sublist = [] |
| 432 | for name in names: |
| 433 | item = FileTreeItem(os.path.join(self.path, name)) |
| 434 | sublist.append(item) |
| 435 | return sublist |
| 436 | |
| 437 | |
| 438 | # A canvas widget with scroll bars and some useful bindings |
no outgoing calls
no test coverage detected