【问题标题】:tkinter button with arguments带参数的 tkinter 按钮
【发布时间】:2021-07-28 07:26:44
【问题描述】:

您好,我尝试构建基于 Python tkinter 的 GUI,我有一个类似的界面:

除了添加按钮“Cargar URL”之外,一切正常,它是一个应该获取参数的函数,“Cargar URL”中的参数是使用“Guardar PDF”创建的链接和左侧的条目

“Cargar URL”出现以下错误 ==> AttributeError: 'StringVar' object has no attribute '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

'

【问题讨论】:

  • 你能分享整个回溯吗?很难说错误在哪里
  • 这是回溯文件“GUI.pyw”,第 149 行,在 botonenviarCargarURL=Button(pes0,text="Cargar URL", font=("Poppins",12),command =lambda: cargar_url(url_2)) 文件“GUI.pyw”,第 54 行,在 cargar_url 响应 = urllib.request.urlopen(My_url) 文件“C:\ProgramData\Anaconda3\lib\urllib\request.py”,第 222 行, 在 urlopen 返回 opener.open(url, data, timeout) 文件“C:\ProgramData\Anaconda3\lib\urllib\request.py”,第 516 行,在 open protocol = req.type AttributeError: 'StringVar' object has no属性“类型”

标签: python function tkinter button


【解决方案1】:

认为问题在于您在创建botonenviarCargarURL Button 时使用的command=

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

...因为url_2 参数被传递给cargar_url(),它期望传递一个字符串而不是StringVar。尝试改用command=lambda: cargar_url(url_2.get())

由于您没有提供Minimal, Reproducible Example,因此我无法自己对此进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-18
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多