用于检查网站的 tkinter 和 urllib 问题
Problem with tkinter and urllib for checking websites
我正在尝试创建一个应用程序,允许您放入多个网站并检查它们是否已关闭。当我 运行 代码 window 打开并且一切正常,直到我单击搜索。然后我收到以下错误
AttributeError: 'Window' 对象没有属性 '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()
您还没有将 text_entry
放在 self
对象上,您无法在 search
函数中访问它。
#Text Box
self.text_entry = Entry(self, width=20, bg="white")
self.text_entry.place(x=0, y=0)
我正在尝试创建一个应用程序,允许您放入多个网站并检查它们是否已关闭。当我 运行 代码 window 打开并且一切正常,直到我单击搜索。然后我收到以下错误 AttributeError: 'Window' 对象没有属性 '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()
您还没有将 text_entry
放在 self
对象上,您无法在 search
函数中访问它。
#Text Box
self.text_entry = Entry(self, width=20, bg="white")
self.text_entry.place(x=0, y=0)