tkinter回调方法未定义全局名称未定义

时间:2017-01-13 02:03:34

标签: python class tkinter callback

我想知道为什么我通过按钮调用方法SavePVParms CloseApplyGraph时没有定义。我知道,如果我把它们放在__init__里面,它们就可以工作,我的问题就解决了,但我不明白为什么它不会按原样运作。我试图找到这个问题并发现这个Python Class: Global/Local variable name not definedPython says global name not defined但请原谅我,如果我不能理解那里的答案。你能帮我理解为什么吗? 背景:这个类是一个顶级窗口,当我按下根窗口中的按钮时弹出窗口(这里我只是直接向你们展示顶层窗口)。当我保存它时,它应该传递所有条目的字典(尚未实现)。 **另外(但如果你不想要,则不需要回答)这是否是OOP中适当的编码风格? (不到1周学习tkinter并从先前的python转移到OOP python)。

import Tkinter as tk
import ttk
class PVDialog(tk.Toplevel):

    def SavePVParms(self):
        print "saved"
        self.destroy()

    def Close(self):
        self.destroy()

    def ApplyGraph(self):
        print 'applied'

    def __init__(self, parent):
        tk.Toplevel.__init__(self, parent)
        print parent
        self.title('PV Parameters Configuration')
        self.geometry('800x480')
        self.resizable(width=False, height=False)
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=8)
        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=2)

    # ====== FRAMES =======

        lb1Frame = tk.LabelFrame(self, text='Electric Properties')
        lb1Frame.grid(row=0, column=0, sticky='ewns', pady=(15, 2), padx=(30,10))

        AdvFrame = tk.LabelFrame(self, text='Thermal Properties')
        AdvFrame.grid(row=1, column=0, sticky='ewns', pady=5, padx=(30,10))

        pcurveFrame = tk.LabelFrame(self, text='PV Curves')
        pcurveFrame.grid(row=0, column=1, sticky='ewns', padx=(0,30), pady=(15,5),rowspan=2)

        ctrlFrame = tk.Frame(self)
        ctrlFrame.grid(row=2, column=0, columnspan=2, sticky='ens', padx=20, pady=(2, 20))

    # ======== PARAMETER INPUT DATA FRAME ============= #

        labelName = tk.Label(lb1Frame, text='Name', anchor='w')
        labelCellType = tk.Label(lb1Frame, text='Cell Type', anchor='w')
        labelPower = tk.Label(lb1Frame, text='Rated Power [W]', anchor='w')
        labelOV = tk.Label(lb1Frame, text='Open Voltage [V]', anchor='w')
        labelSCC = tk.Label(lb1Frame, text='Short-circuit Current [A]', anchor='w')
        labelMPC = tk.Label(lb1Frame, text='Maximum Point Current [I]', anchor='w')
        labelMPV = tk.Label(lb1Frame, text="Maximum point Voltage [V]", anchor='w')
        labelSeries = tk.Label(lb1Frame, text='Cells in series', anchor='w')
        labelPallel = tk.Label(lb1Frame, text='Cells in parallel', anchor='w')
        labelNOCT = tk.Label(AdvFrame, text='NOCT', anchor='w')
        labelThC = tk.Label(AdvFrame, text='Current T factor [%/K]', anchor='w')
        labelThV = tk.Label(AdvFrame, text='Voltage T factor [%/K]', anchor='w')
        labelThP = tk.Label(AdvFrame, text='Power T factor [%/K]', anchor='w')

        labelName.grid(row=0, sticky='ew', padx=10,  pady=2)
        labelCellType.grid(row=1, sticky='ew', padx=10,  pady=2)
        labelPower.grid(row=2, sticky='ew', padx=10,  pady=2)
        labelOV.grid(row=3, sticky='ew', padx=10,  pady=2)
        labelSCC.grid(row=4, sticky='ew', padx=10,  pady=2)
        labelMPC.grid(row=5, sticky='ew', padx=10,  pady=2)
        labelMPV.grid(row=6, sticky='ew', padx=10, pady=2)
        labelSeries.grid(row=7, sticky='ew', padx=10,  pady=2)
        labelPallel.grid(row=8, sticky='ew', padx=10,  pady=2)
        labelNOCT.grid(row=9 , sticky='ew', padx=10,  pady=2)
        labelThC.grid(row=10, sticky='ew', padx=10,  pady=2)
        labelThV.grid(row=11, sticky='ew', padx=10,  pady=2)
        labelThP.grid(row=12, sticky='ew', padx=10,  pady=2)

        entryName = ttk.Entry(lb1Frame, show='Default')
        entryCellType = ttk.Combobox(lb1Frame)
        entryPower = ttk.Entry(lb1Frame)
        entryOV = ttk.Entry(lb1Frame)
        entrySCC = ttk.Entry(lb1Frame)
        entryMPC = ttk.Entry(lb1Frame)
        entryMPV = ttk.Entry(lb1Frame)
        entrySeries = ttk.Entry(lb1Frame)
        entryPallel = ttk.Entry(lb1Frame)
        entryNOCT = ttk.Entry(AdvFrame, width=23)
        entryThC = ttk.Entry(AdvFrame, width=23)
        entryThV = ttk.Entry(AdvFrame, width=23)
        entryThP = ttk.Entry(AdvFrame, width=23)

        entryName.grid(row=0, column=1, sticky='ew')
        entryCellType.grid(row=1, column=1, sticky='ew')
        entryPower.grid(row=2, column=1, sticky='ew')
        entryOV.grid(row=3, column=1, sticky='ew')
        entrySCC.grid(row=4, column=1, sticky='ew')
        entryMPC.grid(row=5, column=1, sticky='ew')
        entryMPV.grid(row=6, column=1, sticky='ew')
        entrySeries.grid(row=7, column=1, sticky='ew')
        entryPallel.grid(row=8, column=1, sticky='ew')
        entryNOCT.grid(row=9, column=1, sticky='w', padx=(26,0))
        entryThC.grid(row=10, column=1, sticky='w', padx=(26,0))
        entryThV.grid(row=11, column=1, sticky='w', padx=(26,0))
        entryThP.grid(row=12, column=1, sticky='w', padx=(26,0))

    # ==== BUTTON COMANDS ======
        saveBttn = ttk.Button(ctrlFrame, text='Save', command=SavePVParms)
        closBttn = ttk.Button(ctrlFrame, text='Cancel', command=Close)
        applyBttn = ttk.Button(ctrlFrame, text='Apply', command=ApplyGraph)
        saveBttn.grid()
        closBttn.grid(row=0, column=1, padx=10)
        applyBttn.grid(row=0, column=2, padx=(0, 10))

# ======== RUNNIG THE CLIENT ============
root = tk.Tk()
dialog = PVDialog(root)
root.mainloop()

更新:当我正在提交这个问题时,我记得__init__是第一个在类中读取和执行的东西,所以如果我把它们定义为什么就定义了在__init__。我仍然不明白为什么python如果在外面__init__不能读取全班的话就不能打电话给他们?那我怎么称那个self.method之外的__init__呢?谢谢!

1 个答案:

答案 0 :(得分:0)

SavePVParmsCloseApplyGraph方法属于您的类,因此您需要使用self.告诉解释器,您要使用您班级中的方法

saveBttn = ttk.Button(..., command=self.SavePVParms) 

对于编码标准,你应该检查PEP 8,但只要你的风格与你的风格一致,它就不应该那么重要。