我不明白这段代码有什么问题,它一直在说:AttributeError: 'NoneType' object has no attribute 'get'

问题描述

我不明白这段代码有什么问题,它一直在说:

AttributeError: 'nonetype' 对象没有属性 'get'

代码

# initial variables

from tkinter import *
import tkinter as tk
import webbrowser

window = Tk()

new = 1

url = 'https://www.youtube.com/watch?v=wb2yOsgEJJg'

window.title("Youtube link dowloader")

window.geometry('1920x1080')

# title setup

title = Label(window,text = 'Youtube Video Downloader',).pack()

title.grid(column=0,row=0)

# TextBox

searchbar = Entry(window,width=10).pack()

searchbar.grid(column=0,row=0)

# Function to open the link in a browser

def openweb():
    url = searchbar.get()
    webbrowser.open(url,new=new)

# button setup

btn = Button(window,text = 'click this button to go to converter',height = 10,width = 50,command=openweb).pack()

btn.grid(column=0,row=3)

window.mainloop()

解决方法

在没有 pack() 的情况下对我来说效果很好,就像这样:

import webbrowser
from tkinter import *

window = Tk()
new = 1
url = 'https://www.youtube.com/watch?v=wb2yOsgEJJg'
window.title("Youtube link dowloader")
window.geometry('1920x1080')

title = Label(window,text='Youtube Video Downloader')
title.grid(column=0,row=0)
searchbar = Entry(window,width=10)
searchbar.grid(column=0,row=0)

def openweb():
    url = searchbar.get()
    webbrowser.open(url,new=new)


btn = Button(window,text='click this button to go to converter',height=10,width=50,command=openweb)

btn.grid(column=0,row=3)
window.mainloop()