首页 > 解决方案 > tkinter 和 urllib 检查网站的问题

问题描述

我正在尝试创建一个应用程序,允许您放入多个网站并检查它们是否已关闭。当我运行代码时,窗口打开并且一切正常,直到我点击搜索。然后我收到以下错误 AttributeError: 'Window' object has no attribute 'text_entry' 任何帮助将不胜感激

#Import everything from tkinter
from tkinter import *

import urllib.request

#Main window
class Window(Frame):

    #Master Widget
    def __init__(self, master = None):
        Frame.__init__(self, master)
        self.master = master
        self.init_window()

    #Creation of init_window
    def init_window(self):
        # changing the title of our master widget
        self.master.title("GUI")
        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)

        #Menu
        menu = Menu(self.master)
        self.master.config(menu=menu)

        #File Menu option
        file = Menu(menu)
        file.add_command(label="Exit", command=self.client_exit)
        menu.add_cascade(label="File", menu=file)

        #Text Box
        text_entry = Entry(self, width=20, bg="white")
        text_entry.place(x=0, y=0)

        #Submit button
        searchButton = Button(self, text='SUBMIT', width=6, 
command=self.search)
        searchButton.place(x=200, y=30)

        #Output Box
        output = Text(self, width=20, height=1, bg="white")
        output.place(x=0, y=50)

    def search(self):
        entered_text = self.text_entry.get()
        output.delete(0.0, END)
        definition=(int(urllib.request.urlopen(entered_text).getcode()))
        output.insert(END, definition)

root = Tk()
#Size of the window
root.geometry("400x300")
#Window instance
app = Window(root)
#Show and mainloop
root.mainloop()

标签: pythontkinterpython-3.7

解决方案


您还没有放置text_entry对象,并且无法在功能self中访问它。search

    #Text Box
    self.text_entry = Entry(self, width=20, bg="white")
    self.text_entry.place(x=0, y=0)

推荐阅读