STG
STG

Reputation: 15

tkinter button with arguments

Hi I trying to build a GUI based on Python tkinter, I have an Interface like : enter image description here

All works fine except when I add the Button "Cargar URL", it is a function that should get a parameter, the parameter in "Cargar URL" is the link created with "Guardar PDF" and the entry to the left

I have got the following Error with "Cargar URL" ==> AttributeError: 'StringVar' object has no attribute 'type'

I am usgin the following script:

## 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()

The entire error is:

 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

'

Upvotes: 0

Views: 342

Answers (1)

martineau
martineau

Reputation: 123393

I think the problem is with the command= you're using when creating the botonenviarCargarURL Button:

botonenviarCargarURL=Button(pes0,text="Cargar URL", font=("Poppins",12),
                            command=lambda: cargar_url(url_2))

…because of the url_2 argument being passed to cargar_url() which expects to passed a string not a StringVar. Try using command=lambda: cargar_url(url_2.get()) instead.

I can't test this myself since you haven't provided a Minimal, Reproducible Example.

Upvotes: 2

Related Questions