python - 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()
解决方案
您还没有放置text_entry
对象,并且无法在功能self
中访问它。search
#Text Box
self.text_entry = Entry(self, width=20, bg="white")
self.text_entry.place(x=0, y=0)
推荐阅读
- python - 为什么洛杉矶的UTC偏移量是16:07?
- azure-functions - 选择 `azure function` 与 `http request` 操作是否有性能优势?
- sql - T-SQL If 条件在 select 内
- android - 长时间暂停后写入时,Android蓝牙发送数据交换
- clojure - Datomic Ions:连接到本地数据库
- centos - CentOS 默认 confiteor repos 和 vault (online repos) 之间有什么关系
- ios - 将特定类型的数据显示到表格视图
- r - 我如何安装这个包“JohnsonDistribution”
- c# - 仅将包含数据的第一列从一个 DataTable 复制到另一个新 DataTable
- java - BLOG 语言成绩差了一小部分