Tkinter使用Listbox获取错误

时间:2015-11-18 18:49:46

标签: python tkinter

我正在尝试使用Listbox功能允许用户在我编写的程序中为x和y轴选择一个选项。我一直收到这个错误:

Traceback (most recent call last):
  File "tkkkk2.py", line 72, in <module>
    my_gui = MyFirstGUI(root)
  File "tkkkk2.py", line 51, in __init__
    listbox = Listbox(master)
NameError: global name 'Listbox' is not defined

以下是代码:

import random
from Tkinter import Tk, Label, Button, Entry, StringVar, DISABLED, NORMAL, END, W, E
from Tkinter import BOTH, END, LEFT
class MyFirstGUI:
    def __init__(self, master):
        self.master = master
        master.title("Python Scraper")

        self.label1 = Label(master, text="Please enter a hashtag (#hashtag) to search twitter for.")
        self.label1.pack()

        #Box to input hashtag

        v = StringVar()
        e = Entry( master, textvariable = v)
        e.pack()
        #gets the value from the box.
        #We could do this on the button call and pass all values on
        #to the real program.
        s = v.get()

        self.label2 = Label(master, text = "How many results would you like per search? Recommended: 200-1000.")
        self.label2.pack()

        #Box to enter results per search

        results = StringVar()
        r = Entry( master, textvariable = results)
        r.pack()

        if r.get() != '':
            other_variable = int(r.get())

        self.label3= Label(master, text = "How many times would you like Twitter scraped? Warning: do not exceed the data cap! Recommended 6 or less times")
        self.label3.pack()

        #box to enter number of times to scrape twitter
        times = StringVar()
        t = Entry( master, textvariable = results)
        t.pack()

        if t.get() != '':
            other_variable_times = int(t.get())


        self.label4 = Label(master, text = "What would you like on your x-axis?")
        self.label4.pack()

        #drop down box with the 5 options

        #!!PROBLEM AREA!!!!
        List1 = Listbox(root)
        List1.insert(1,'python')
        List1.pack()


        self.label5 = Label(master, text = "What would you like on your y-axis")
        self.label5.pack()

        #dropdown box with the same five options as above

        self.search_button = Button(master, text="Search", command=self.search)
        #self.search_button.pack()



    def search(self):

    #This should take the inputs from the boxes and selections from the x and y axis and 
    #enter it into another program that will be pasted below. I don't want to share due to 
    #OAuth credentials etc..
        print("Greetings!")

root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()

1 个答案:

答案 0 :(得分:2)

错误告诉您Listbox未定义。您无法在任何地方导入它,而且您无法在任何地方进行定义。

这是大多数人建议一次导入所有Tkinter的一个很好的理由:

import Tkinter as tk
...
root = tk.Tk()
...
listbox = tk.Listbox(master)
...