NameError: 全局名称 'END' 未定义 [英] NameError: global name 'END' is not defined

查看:31
本文介绍了NameError: 全局名称 'END' 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这段关于滚动条的代码运行良好.

I have found this code about scrollbar is just working fine.

from tkinter import *

master = Tk()

scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(master, yscrollcommand=scrollbar.set)
for i in range(10000):
    listbox.insert(END, str(i))
listbox.pack(side=LEFT, fill=BOTH)

scrollbar.config(command=listbox.yview)

mainloop()

我尝试在我的代码中像这样使用它:

I try to use it in my code like this:

import tkinter as tk

class interface(tk.Frame):
    def __init__(self,den):
        self.tklist() 
        #in my code, tklist is not called here. I called it here to minimize the code
        #there are stuff in here also

    def tklist(self):
        scrollbar = tk.Scrollbar(den)
        self.lst1 = tk.Listbox(den, selectmode="SINGLE", width="100", yscrollcommand=scrollbar.set)
        for i in range(1000):
            self.lst1.insert(END, str(i))
        self.lst1.pack(side=LEFT, fill=BOTH)
        scrollbar.config(command=lst1.yview)

den = tk.Tk()
den.title("Search")

inter = interface(den)

den.mainloop()

但是当我运行上面的代码时,我在插入行出现错误.

But when I ran above code, I got an error on insertion line.

NameError: global name 'END' is not defined

顺便说一下,我试图找到文档,来自 effbot 的链接 是最近的我明白了,但仍然不明白出了什么问题.

By the way, I tried to find documentation and a link from effbot is the closest I got but still couldn't understand what is wrong.

推荐答案

ENDLEFTBOTH 都驻留在 tkinter 命名空间.因此,它们需要通过在它们之前放置 tk. 来限定:

END, LEFT, and BOTH all reside in the tkinter namespace. Thus, they need to be qualified by placing tk. before them:

for i in range(1000):
    self.lst1.insert(tk.END, str(i))
self.lst1.pack(side=tk.LEFT, fill=tk.BOTH)
scrollbar.config(command=lst1.yview)

或者,您可以根据需要显式导入它们:

Or, you could import them explicitly if you want:

from tkinter import BOTH, END, LEFT

这篇关于NameError: 全局名称 'END' 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆