You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter中无法从Entry组件获取输入的问题求助

解决Tkinter中Entry组件访问的NameError问题

错误信息

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "window1.py", line 46, in GButton_109_command
    marks = Line1.get()
            ^^^^^
NameError: name 'Line1' is not defined

原代码

import webbrowser
import tkinter as tk
import tkinter.font as tkFont


class App:
    def __init__(self, root):
        root.title("Підрахунок тематичної оцінки")
        # setting window size
        width = 300
        height = 200
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)

        Line1 = tk.Entry(root)
        Line1["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times', size=10)
        Line1["font"] = ft
        Line1["fg"] = "#333333"
        Line1["justify"] = "center"
        Line1["text"] = "Введіть оцінки через пробіл: "
        Line1.place(x=10, y=70, width=276, height=30)

        GLabel_323 = tk.Label(root)
        ft = tkFont.Font(family='Times', size=10)
        GLabel_323["font"] = ft
        GLabel_323["fg"] = "#333333"
        GLabel_323["justify"] = "center"
        GLabel_323["text"] = "Підрахунок тематичної оцінки"
        GLabel_323.place(x=10, y=20, width=278, height=30)

        GButton_109 = tk.Button(root)
        GButton_109["bg"] = "#f0f0f0"
        ft = tkFont.Font(family='Times', size=10)
        GButton_109["font"] = ft
        GButton_109["fg"] = "#000000"
        GButton_109["justify"] = "center"
        GButton_109["text"] = "Submit"
        GButton_109.place(x=110, y=140, width=81, height=30)
        GButton_109["command"] = self.GButton_109_command

    def GButton_109_command(self):
        marks = Line1.get()
        mlist = marks.split()
        # convert each item to int type
        for i in range(len(mlist)):
            mlist[i] = int(mlist[i])

        ar = sum(mlist) / len(mlist)

        print(ar)

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

问题原因

Line1是__init__方法内的局部变量,类的其他方法无法直接访问局部变量,必须将其声明为类的实例属性才能在类的各个方法中共享。

修正方案

只需要两处修改:

  • 在__init__方法中,将Line1 = tk.Entry(root)改为self.Line1 = tk.Entry(root),把Entry组件绑定为实例属性;
  • 在GButton_109_command方法中,将marks = Line1.get()改为marks = self.Line1.get(),通过实例属性访问Entry组件。

修正后的代码

import webbrowser
import tkinter as tk
import tkinter.font as tkFont


class App:
    def __init__(self, root):
        root.title("Підрахунок тематичної оцінки")
        # setting window size
        width = 300
        height = 200
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        root.geometry(alignstr)
        root.resizable(width=False, height=False)

        # 修改:将Line1改为实例属性self.Line1
        self.Line1 = tk.Entry(root)
        self.Line1["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times', size=10)
        self.Line1["font"] = ft
        self.Line1["fg"] = "#333333"
        self.Line1["justify"] = "center"
        self.Line1["text"] = "Введіть оцінки через пробіл: "
        self.Line1.place(x=10, y=70, width=276, height=30)

        GLabel_323 = tk.Label(root)
        ft = tkFont.Font(family='Times', size=10)
        GLabel_323["font"] = ft
        GLabel_323["fg"] = "#333333"
        GLabel_323["justify"] = "center"
        GLabel_323["text"] = "Підрахунок тематичної оцінки"
        GLabel_323.place(x=10, y=20, width=278, height=30)

        GButton_109 = tk.Button(root)
        GButton_109["bg"] = "#f0f0f0"
        ft = tkFont.Font(family='Times', size=10)
        GButton_109["font"] = ft
        GButton_109["fg"] = "#000000"
        GButton_109["justify"] = "center"
        GButton_109["text"] = "Submit"
        GButton_109.place(x=110, y=140, width=81, height=30)
        GButton_109["command"] = self.GButton_109_command

    def GButton_109_command(self):
        # 修改:通过self.Line1访问Entry组件
        marks = self.Line1.get()
        mlist = marks.split()
        # convert each item to int type
        for i in range(len(mlist)):
            mlist[i] = int(mlist[i])

        ar = sum(mlist) / len(mlist)

        print(ar)

if __name__ == "__main__":
    root = tk.Tk()
    app = App(root)
    root.mainloop()

内容的提问来源于stack exchange,提问作者l4nd3r

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 21:45:11