当我使用 class 定义绑定函数时显示条目未定义错误

entry is not defind error showing while i use a class to define the binding function

screenshot of error

当我将绑定函数移动到 class 或另一个模块时,我遇到了屏幕截图中显示的错误

显示为条目未定义..它在没有 class

的情况下工作正常
import tkinter 
from tkinter import *

root = tkinter.Tk()
root.wm_title("speed entering calculator")

class Methods1:
    i=0

    #function for enter
def entering(self,a):
        global i #to inform this function that i is global variable
        try:     # try and exception used to print message if error happen

            value1=int (entry1.get()) #entry1.get()is used to get values which entered in entry box
            value1=i + value1
            label1['text']=value1
            i=value1

        except ValueError as ve:
            print(f'only integer')



    entry1.delete(0, END) # used to clear entry box




ob=Methods1()











#creating canvas
canvas1 = tkinter.Canvas(root, height=400 , width=400, bg="black")
canvas1.pack()

#making entry box for values
entry1 = tkinter.Entry(root, justify="center", )
canvas1.create_window(200,140,height=100,width=100, window=entry1)

value = "the values should be shown here"
label1=Label(root, text =value, height=10) 
label1.pack()




#binding enter key as adding
root.bind('<Return>',ob.entering)






root.mainloop()

我不太擅长编程语言。所以我希望我能从这里找到解决方案

此代码中存在一些问题,尤其是格式问题,但是当我更正缩进时,我没有看到 entry1 未定义的错误。然而,变量 i 是未定义的,方法是静态的,这意味着它不应该在 class 中。一旦我做了一些更改(从 class 中删除函数,然后整体删除 class,代码似乎就可以工作了)。这是有效的代码:

import tkinter 
from tkinter import *

root = tkinter.Tk()
root.wm_title("speed entering calculator")

i = 0


# function for enter
def entering(event):

    global i
    # try and exception used to print message if error happen
    try:
        # get values which are entered in entry box
        value1 = int(entry1.get())
        value1 = i + value1
        label1['text'] = value1
        i = value1
    except ValueError:
        print("only integer")

    # clear entry box
    entry1.delete(0, END)


# creating canvas
canvas1 = tkinter.Canvas(root, height=400, width=400, bg="black")
canvas1.pack()

# making entry box for values
entry1 = tkinter.Entry(root, justify="center")
canvas1.create_window(200, 140, height=100, width=100, window=entry1)

value = "the values should be shown here"
label1 = Label(root, text=value, height=10)
label1.pack()

# binding enter key as adding
root.bind('<Return>', entering)

root.mainloop()

主要问题是试图在对象声明之前引用它们。

重新排列代码解决了这个问题。

另一个主要问题是用于 tkinter

的导入方法

将其更改为标准格式即可解决该问题。

最后 class 中的 i 需要更改为 self.i

现在可以使用了。

import tkinter as tk

root = tk.Tk()
root.title("speed entering calculator")

#creating canvas
canvas1 = tk.Canvas(root, height = 400, width = 400, bg = "black")
canvas1.pack()

#making entry box for values
entry1 = tk.Entry(root, justify = "center")
canvas1.create_window(200, 140, height = 100, width = 100, window = entry1)

value = "the values should be shown here"
label1 = tk.Label(root, text = value, height = 10) 
label1.pack()

class Methods1:
    i = 0

    def entering(self, a):
        try:
            value1 = int(entry1.get()) 
            value1 = self.i + value1
            label1['text'] = value1
            self.i = value1
        except ValueError as ve:
            print(f'only integer')
        entry1.delete(0, tk.END) # used to clear entry box

ob = Methods1()

#binding enter key as adding
root.bind('<Return>', ob.entering)

root.mainloop()