编程语言
首页 > 编程语言> > Python--tkinter.filedialog的学习

Python--tkinter.filedialog的学习

作者:互联网

tkinter.filedialog 的学习

tkinter.filedialog.asksaveasfilename():选择以什么文件名保存,返回文件名

def selectinputPath():
    input_path = asksaveasfilename()
    inputpath1 = inputpath.set(input_path)
    print(input_path)

显示如下:
在这里插入图片描述
在这里插入图片描述

tkinter.filedialog.askopenfilename():选择打开什么文件,返回文件的绝对路径

def selectinputPath():
    input_path = askopenfilename()
    inputpath1 = inputpath.set(input_path)
    print(input_path)

显示如下:
在这里插入图片描述

tkinter.filedialog.askdirectory():选择目录,返回目录名

def selectinputPath():
    input_path = askdirectory()
    inputpath1 = inputpath.set(input_path)
    print(input_path)

显示如下:
在这里插入图片描述

代码如下:

from tkinter import *
import tkinter.messagebox as messagebox
from tkinter.filedialog import *
import time
import os
import shutil


currentTime = time.strftime('%Y%m%d')  #定义时间

def save_Filename():
    saveas_filename = asksaveasfilename()
    save_filename1 = save_filename.set(saveas_filename)
    print(saveas_filename)

def selectPath():
    input_path = askdirectory()
    inputpath1 = inputpath.set(input_path)
    print(input_path)

def select_Absolutepath():
    absolute_path = askopenfilename()
    absolutepath = Absolute.set(absolute_path)
    print(absolute_path)
    
def Copyfilename():
    file_name = askopenfilename()
    file_name1 = os.path.basename(file_name) #取最后文件名
    filename1 = filename.set(file_name1)
    print(file_name)
    target = './'
    Copyfile = shutil.copy(file_name,target) #将文件复制当前目录下

def getname():
    name = name.get()
    print(name)

def allsubmit():
#        text = currentTime + '.txt' 
    with open(currentTime + '.txt', 'w') as f:  #编写文本
        f.write(inputpath.get() + '    '+ filename.get())
    messagebox.showinfo('Success', '恭喜您已成功提交 ')   # 显示对话框

master = Tk()
master.title('my window')
master.geometry('500x400')
menubar = Menu(master)


master.config(menu=menubar)

frame8 = Frame(master) #Frame 窗口部件
frame8.pack(fill=X) #pack()一个布局方法
label8 = Label(frame8, text='Saveas filename') #图形界面上设定标签
label8.grid(row=1, column=0) #grid() 基于网格布局方法
save_filename = StringVar() #定义变量
entryname = Entry(frame8, textvariable=save_filename) #窗口部件(用于输入显示一行文本),在label上设定
entryname.grid(row=1,column=2,ipadx=60,padx=10)
getname = Button(frame8, text='Select', command=save_Filename) #按钮部件(与函数或方法相关联,当按钮被按下时,TK会自动调用相关联的函数或方法)
getname.grid(row=1,column=4)


#input path放在frame1      
frame1 = Frame(master)
frame1.pack(fill=X)
#输入input gds
label1 = Label(frame1, text='Input  path')
label1.grid(row=1, column=0)
inputpath = StringVar()
entryname = Entry(frame1, textvariable=inputpath)
entryname.grid(row=1,column=2,ipadx=60,padx=10)
#按钮
getname = Button(frame1, text='Select', command=selectPath)
getname.grid(row=1,column=4)

frame3 = Frame(master)
frame3.pack(fill=X)
label3 = Label(frame3, text='Absolute_Path')
label3.grid(row=1, column=0)
Absolute = StringVar()
entryname = Entry(frame3, textvariable=Absolute)
entryname.grid(row=1,column=2,ipadx=60,padx=10)
getname = Button(frame3, text='Select', command=select_Absolutepath)
getname.grid(row=1,column=4)


#gds name放在frame2中
frame2 = Frame(master)
frame2.pack(fill=X)
        
label2 = Label(frame2, text='File  Name')
label2.grid(row=1,column=0)
#输入gds name
filename = StringVar()
entryname = Entry(frame2, textvariable=filename)
entryname.grid(row=1,column=2,ipadx=60,padx=10)
getname = Button(frame2, text='Select', command=Copyfilename)
getname.grid(row=1, column=3)

frame4 = Frame(master)
frame4.pack()
submit = Button(frame4, text='submit', command=allsubmit)
submit.grid()

frame6 = Frame(master)
frame6.pack()  #设定空白标签
frame7 = Frame(master)
frame7.pack()

frame5 = Label(master, bg='red',width=50)
frame5.pack()
    
master.mainloop() #主窗口循环显示

丿孙小飞 发布了2 篇原创文章 · 获赞 0 · 访问量 30 私信 关注

标签:tkinter,Python,column,grid,master,path,filedialog,input,row
来源: https://blog.csdn.net/qq_43670393/article/details/103937869