I have a problem with my code. I know the error in the code, but I don't know how to fix it.
The programming language is Python.
This is my project:
from tkinter import *
import os
from tkinter import filedialog
def saveFile():
file = filedialog.asksaveasfile(defaultextension='.png',
filetypes=[
("Png file", ".png"),
("Text file",".txt"),
("All files", ".*"),
])
if file is None:
return
filetext = url.get()
file.write(filetext)
file.close()
window = Tk()
window.title("Download from browsers")
window.wm_attributes('-toolwindow', 'True')
Tk_Width = 350
Tk_Height = 150
positionRight = int( window.winfo_screenwidth()/2 - Tk_Width/2 )
positionDown = int( window.winfo_screenheight()/2 - Tk_Height/2 )
window.geometry("{}x{}+{}+{}".format(330,140,positionRight, positionDown))
urltxt = Label(window, text = "Download Url").place(x = 10, y = 10)
url = Entry(window, width=30)
url.insert(0,"")
url.grid(row=1, column=0, padx=100, pady=10)
button = Button(text='Save as',command=saveFile)
button.grid(row=2, column=0, padx=100, pady=10)
def startdownload():
os.system(f"powershell -c \"Invoke-WebRequest -Uri '{url.get()}' -OutFile '{button.get()}'\"")
os.system("msg * s")
btnSendstartdownload = Button(window, text="download", width=20, command=startdownload)
btnSendstartdownload.grid(row=3, column=0, padx=10, pady=10)
window.mainloop()
The wrong part here
os.system(f"powershell -c \"Invoke-WebRequest -Uri '{url.get()}' -OutFile '{button.get()}'\"")
{button.get()} is the button to save the file path
This is a picture of the program
{button.get()} is supposed to fetch me the file save path
But it shows me an error
I think the problem appears because I am trying to fetch the file path from a button
Does anyone know how to get the file save path from this code?
This is the error message
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\user\Desktop\1\a.py", line 42, in startdownload
os.system(f"powershell -c \"Invoke-WebRequest -Uri '{url.get()}' -OutFile '{button.get()}'\"")
AttributeError: 'Button' object has no attribute 'get'
Try,
window.filename = filedialog.asksaveasfile(defaultextension='.png',
filetypes=[
("Png file", ".png"),
("Text file",".txt"),
("All files", ".*"),
])
file= root.filename
where window=Tk()
it worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.