嗨,我正在尝试基于Python tkinter构建GUI,我有一个如下界面:
除了我添加“Cargar URL”按钮外,所有功能都可以正常工作,“Cargar URL”中的参数是使用“Guardar PDF”创建的链接和左侧的条目
“Cargar URL”出现以下错误==>;AttributeError:“StringVar”对象没有属性“type”
我正在使用以下脚本:
## librerias
from tkinter import font
import nltk
#nltk.download('punkt')
#nltk.download('stopwords')
#nltk.download('wordnet')
#nltk.download('averaged_perceptron_tagger')
from nltk.tokenize import sent_tokenize
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.corpus import wordnet
#Importar librerias adicionales
import matplotlib as plt
import pandas as pd
import string
from bs4 import BeautifulSoup
import urllib.request
import re
import PyPDF2 as pdf
#Librerias de GUI
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import warnings
pd.set_option('display.max_columns', 100)
############################################### INICIA GUI #################################################################################
# se crea Raiz de GUI / dimensiones
root = tk.Tk()
root.title('Identificador de regímenes de metaforas')
root.config(bg='pink')
root.geometry("1500x800")
# Funciones para lso botnores de inicio
def GuardarURL():
url=rutaalmacenamientotituloURLVALUE.get()
print(url)
messagebox.showinfo("State","Success")
return url_2.set(url)
# Cargar Documento Base relacionado con el ente territorial a partir de url
def cargar_url(My_url):
response = urllib.request.urlopen(My_url)
html = response.read()
soup = BeautifulSoup(html,"html5lib")
texto_base = soup.get_text(strip=False)
print(texto_base)
messagebox.showinfo("State","Success")
return texto_base
def GuardarRUTALOCAL():
ruta_pdf=rutaalmacenamientotituloLOCALVALUE.get()
print(ruta_pdf)
messagebox.showinfo("State","Success")
return ruta_pdf_2.set(ruta_pdf)
def GuardarENTE():
ente_territorial=rutaalmacenamientoENTEVALUE.get()
print(ente_territorial)
messagebox.showinfo("State","Success")
return ente_territorial_2.set(ente_territorial)
ruta_pdf_2 = tk.StringVar()
ruta_pdf_2.set('')
url_2 = tk.StringVar()
url_2.set('')
ente_territorial_2 =tk.StringVar()
ente_territorial_2.set('')
# Se crea las pestañas usando lib tkk
notebook=ttk.Notebook(root)
notebook.pack(fill='both', expand='yes')
pes0=ttk.Frame(notebook)
pes1=ttk.Frame(notebook)
pes2=ttk.Frame(notebook)
notebook.add(pes0, text='Inicio')
notebook.add(pes1, text='Análisis de ducumento base')
notebook.add(pes2, text='Regímenes de metáforas')
############################################### BODY GUI #################################################################################
#-----------------------------URL----------------------
#titulo ruta almacenamiento
rutaalmacenamientotituloURL=tk.Label(pes0, text="Ruta Documento base URL")
rutaalmacenamientotituloURL.place(x=300, y=150, height=40,width=300)
#Entrada de ruta de URL
rutaalmacenamientotituloURLVALUE=tk.Entry(pes0, width=500)
rutaalmacenamientotituloURLVALUE.place(x=560, y=150, height=40,width=300)
rutaalmacenamientotituloURLVALUE.insert(END,'https://eur-lex.europa.eu/legal-content/EN/TXT/HTML/?uri=CELEX:52017DC2025&from=EN')
#boton enviar URL
botonenviarURL=Button(pes0,text="Guardar URL", font=("Poppins",12),command=GuardarURL)
botonenviarURL.place(x=865, y=155)
#----------------------------LOCAL----------------------
#titulo ruta almacenamiento
rutaalmacenamientotituloLOCAL=tk.Label(pes0, text="Ruta Documento base Documento local")
rutaalmacenamientotituloLOCAL.place(x=300, y=200, height=40,width=300)
#Entrada de ruta LOCAL
rutaalmacenamientotituloLOCALVALUE=tk.Entry(pes0, width=500)
rutaalmacenamientotituloLOCALVALUE.place(x=560, y=200, height=40,width=300)
rutaalmacenamientotituloLOCALVALUE.insert(END,'Doc_base.pdf')
#boton enviar ruta LOCAL
botonenviarRUTA=Button(pes0,text="Guardar PDF", font=("Poppins",12),command=GuardarRUTALOCAL)
botonenviarRUTA.place(x=865, y=200)
#-----------------------------Ente Territorial------------
#titulo ruta almacenamiento
rutaalmacenamientotituloENTE=tk.Label(pes0, text="Ente Territorial")
rutaalmacenamientotituloENTE.place(x=300, y=250, height=40,width=300)
#Entrada Ente Territorial
rutaalmacenamientoENTEVALUE=tk.Entry(pes0, width=500)
rutaalmacenamientoENTEVALUE.place(x=560, y=250, height=40,width=300)
rutaalmacenamientoENTEVALUE.insert(END,'Europe')
#boton enviar Ente Territorial
botonenviarENTE=Button(pes0,text="Guardar ENTE", font=("Poppins",12),command=GuardarENTE)
botonenviarENTE.place(x=865, y=250)
#-----------------------------Cargar URL----------------------
#boton Cargar URL
botonenviarCargarURL=Button(pes0,text="Cargar URL", font=("Poppins",12),command=lambda: cargar_url(url_2))
botonenviarCargarURL.place(x=865, y=500)
root.mainloop()
整个错误是:
File "GUI.pyw", line 149, in <lambda>
botonenviarCargarURL=Button(pes0,text="Cargar URL", font=("Poppins",12),command=lambda: cargar_url(url_2))
File "GUI.pyw", line 54, in cargar_url
response = urllib.request.urlopen(My_url)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 516, in open
protocol = req.type
AttributeError: 'StringVar' object has no attribute 'type
"
我认为问题出在创建}时使用的
botonenviarCargarURL
{command=
上:…因为
url_2
参数被传递给cargar_url()
,它希望传递的是字符串而不是StringVar
。尝试改用command=lambda: cargar_url(url_2.get())
我无法亲自测试,因为您没有提供Minimal, Reproducible Example
相关问题 更多 >
编程相关推荐