赋值前引用的Tkinter局部变量“calcButton”

2024-09-24 04:20:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个代码:

#!/usr/bin/python
-*- coding: utf-8 -*-

from Tkinter import *
from ttk import Frame, Button, Style


class Example(Frame):

def __init__(self, parent):
    Frame.__init__(self, parent)   

    self.parent = parent

    self.initUI()

def initUI(self):

    self.parent.title("Multiplication")
    self.style = Style()
    self.style.theme_use("clam")

    self.pack(fill=BOTH, expand=1)

    E1 = Entry(bd =5)
    E1.place(x=0, y=0)
    E2 = Entry(bd=5)
    E2.place(x=125, y=0)
    E3 = Entry(bd=5)
    E3.place(x=62.5, y=25)
    calcButton = Button(self, text="Button", command=calcButton.calculate)
    calcButton.place(x=50, y=50)
def calculate(calcButton):
    a = E1.get()
    b = E2.get()
    c = E3.get()


def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    main()

我得到了一个错误:

Traceback (most recent call last): File "E:\Python27\tkinter", line 48, in <module> main() File "E:\Python27\tkinter", line 43, in main app = Example(root) File "E:\Python27\tkinter", line 15, in __init__ self.initUI() File "E:\Python27\tkinter", line 31, in initUI calcButton = Button(self, text="Button", command=calcButton.calculate) UnboundLocalError: local variable 'calcButton' referenced before assignment

很抱歉缩进不正确,这个代码粘贴很困难。 我看过重复的问题,也试过他们说的话,但都没用。 感谢任何帮助! 谢谢。在


Tags: inselfmaintkinterdeflineplacebutton
1条回答
网友
1楼 · 发布于 2024-09-24 04:20:46

在这一行中,您使用的是:

calcButton = Button(self, text="Button", command=calcButton.calculate)

其中,calcButton.calculate被调用以将其分配给calcButton,但它尚未声明。在

这可能是:

^{pr2}$

相关问题 更多 >