【发布时间】:2021-12-07 18:33:16
【问题描述】:
这是我目前所拥有的:
tkinter.py
from tkinter import *
import tkinter.filedialog
import os
import sys
root = Tk()
root.title('Download File')
root.iconbitmap('C:/Users/londo/Downloads/tkinter/Logo.ico')
root.geometry("800x600")
pyexec = sys.executable
def resize():
root.geometry("800x800")
myLabel = Label(root, text="Rename the file to what ever you want, and download it!")
myLabel.pack(pady=20)
def open():
PathPy = tkinter.filedialog.askopenfilename(title="Open a file",file=[('downloadtkinter.py')])
os.system('%s %s' % (pyexec, PathPy))
my_button = Button(root, text="Download", command=open)
my_button.pack(pady=20)
root.mainloop()
错误:
Traceback (most recent call last):
File "D:\Users\londo\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\londo\Downloads\tkinter\tkintertest2.py", line 21, in open
PathPy = tkinter.filedialog.askopenfilename(title="Open a file",file=[('downloadtkinter.py')])
File "D:\Users\londo\AppData\Local\Programs\Python\Python39\lib\tkinter\filedialog.py", line 384, in askopenfilename
return Open(**options).show()
File "D:\Users\londo\AppData\Local\Programs\Python\Python39\lib\tkinter\commondialog.py", line 46, in show
s = w.tk.call(self.command, *w._options(self.options))
_tkinter.TclError: bad file type "downloadtkinter.py", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"
下载tkinter.py
from tkinter as tk
import os
frame = tk.Tk()
frame.title('Download File Here')
frame.iconbitmap('C:/Users/londo/Downloads/tkinter/Logo.ico')
frame.geometry("800x800")
def resize():
root.geometry("800x800")
def printInput():
inp = inputtxt.get(1.0, "end-1c")
lbl.config(text = "Provided Input: "+inp)
myLabel = Label(root, text="Rename the file to what ever you want, and download it!")
myLabel.pack(pady=20)
inputtxt = tk.Text(frame,
hight = 1,
width = 20)
inputtxt.pack()
printButton = tk.Button(frame,
text = "Print",
command = printInput)
printButton.pack()
lbl = tk.Label(frame, text = "")
lbl.pack()
frame.mainloop()
我不知道如何用文本框中的文本更改文件名并上传文件,如果可能的话,请告诉我。谢谢,London(我对 tkinter 很陌生,这个项目对于我的技能水平来说可能太先进了)
【问题讨论】:
-
不要使用
tkinter.py作为脚本的名称。 -
该错误会告诉您确切的问题所在。您需要使用
filetypes=[("Python Script", "*.py")]之类的东西,而不是file=[("downloadtkinter.py")]。
标签: python python-3.x tkinter file-upload file-rename