2015-08-28 67 views
1

我試圖在選擇另一個小部件時更新python tkinter應用程序中的標籤。引用python tkinter應用程序中的全局對象

我創建了一個小工具來演示這個問題。該工具將創建一個頂部帶有標籤的小型GUI。該標籤應顯示所選框的編號。

Image of tool

問題是,當由鼠標點擊選擇的盒子編號,沒有在頂標籤顯示的數字。點擊框號應該調用setSelected,它應該調用app.setLabel(string)。但是,我收到錯誤「全球名稱」應用程序「未定義」

如何使對象「應用程序」全局?

#!/usr/bin/env python 
import Tkinter 

def setSelected(string): 
    app.setLabel(string) 

class Gui(): 
    Boxes = [] 

    def __init__(self): 
     self._root = Tkinter.Tk() 
     self._root.protocol("WM_DELETE_WINDOW", self._applicationExit) 
     self._string = Tkinter.StringVar() 
     self.setLabel('None') 
     self._label = Tkinter.Label(self._root, textvariable = self._string, 
      width = 10) 
     self._label.grid(row = 0, column = 0, padx = 5, pady = 5) 
     self._createBoxOverview() 
     self._root.mainloop() 

    def _applicationExit(self, event = None): 
     self._root.destroy() 

    def _createBoxOverview(self): 
     _frame = Tkinter.LabelFrame(self._root, text = 'Boxes') 
     for _id in range(4): 
      self.Boxes.append(Box(_frame, _id)) 
      self.Boxes[_id].grid(row = 0, column = _id) 
     _frame.grid(row = 1, column = 0, padx = 5, pady = 5) 

    def setLabel(self, string): 
     self._string.set(string) 

class Box(Tkinter.Label): 
    def __init__(self, master, id): 
     Tkinter.Label.__init__(self, master) 
     self._id = str(id) 
     self._text = Tkinter.StringVar() 
     self._text.set(self._id) 
     self.config(textvariable = self._text, width = 3) 
     self.bind("<Button-1>", self._onSelect) 

    def _onSelect(self, event): 
     setSelected(self._id) 

if __name__ == '__main__': 
    app = Gui() 

回答

1

的問題是,你正在創建rootTk()應用程序),並調用root.mainloop(),裏面__init__()本身,所以app沒有完全生成,因爲如果你從__init__()返回它只會生成,但你直到你關閉應用程序纔會這樣做。

您的情況最容易的解決方案將移動root對象outisde Gui()類。示例 -

import Tkinter 

def setSelected(string): 
    app.setLabel(string) 

class Gui(): 
    Boxes = [] 

    def __init__(self, root): 
     self._root = root 
     self._root.protocol("WM_DELETE_WINDOW", self._applicationExit) 
     self._string = Tkinter.StringVar() 
     self.setLabel('None') 
     self._label = Tkinter.Label(self._root, textvariable = self._string, 
      width = 10) 
     self._label.grid(row = 0, column = 0, padx = 5, pady = 5) 
     self._createBoxOverview() 

    def _applicationExit(self, event = None): 
     self._root.destroy() 

    def _createBoxOverview(self): 
     _frame = Tkinter.LabelFrame(self._root, text = 'Boxes') 
     for _id in range(4): 
      self.Boxes.append(Box(_frame, _id)) 
      self.Boxes[_id].grid(row = 0, column = _id) 
     _frame.grid(row = 1, column = 0, padx = 5, pady = 5) 

    def setLabel(self, string): 
     self._string.set(string) 

class Box(Tkinter.Label): 
    def __init__(self, master, id): 
     Tkinter.Label.__init__(self, master) 
     self._id = str(id) 
     self._text = Tkinter.StringVar() 
     self._text.set(self._id) 
     self.config(textvariable = self._text, width = 3) 
     self.bind("<Button-1>", self._onSelect) 

    def _onSelect(self, event): 
     setSelected(self._id) 

if __name__ == '__main__': 
    root = Tkinter.Tk() 
    app = Gui(root) 
    root.mainloop() 
相關問題