2015-11-18 2 views
-1

Я пытаюсь использовать функцию Listbox, чтобы пользователь мог выбрать опцию для оси x и y в написанной мной программе. Я получаю эту ошибку:Tkinter using Ошибка получения списка

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() 

ответ

2

ошибка говорит вам Listbox неопределен. Вы не импортируете его нигде, и вы не определяете его нигде.

Это хорошая причина, почему большинство людей рекомендуют импортировать все Tkinter сразу:

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

Урок. Спасибо. – Davep

Смежные вопросы