提问人:Billy 提问时间:11/16/2023 更新时间:11/16/2023 访问量:33
Tkinter - 导出附件
Tkinter - export attachment
问:
因此,我使用 Tkinter 创建了一个用户表单,该表单有一个按钮,可让您附加文件的文件夹路径。问题是我想按下一个按钮来获取文件夹路径,而不是立即打开文件对话框。我还没有找到在按下按钮后从“openFile”函数中保存“filePath”变量的方法。
from PIL import ImageTk, Image
import time
import pyautogui as py
import tkinter as tk
from tkinter import ttk,Label, messagebox, font, filedialog
from tkinter import *
import datetime
import pickle
import json
# Create user form
root = Tk()
root.title('ServiceNow Access Request User Form')
root.configure(bg="#f6f6f8")
root.geometry("500x470")
# Combo & List box items
personnelStatus = ["-- None --","Regular Employee","Temporary Employee","Non Employee"]
requestType = ["-- None --","Access Change","Hire","ID Renewal"]
employeeType = ["-- None --","Contractor","Consultant","Vendor","Volunteer"]
accessChange = ["Active Directory", "Financial Systems"]
# Format
bigFont = font.Font(family='Microsoft Sans Serif',size= 12)
root.option_add("*TCombobox*Listbox*Font", bigFont)
# AR Image
image = tk.PhotoImage(file="C:\\Users\\E100676\\Pictures\\AccessRequest.png")
label = tk.Label(root,image=image, bg="#f6f6f8")
label.grid(column=1, row=1, sticky="w")
# Personnel status combo box (PS)
PSlabel = Label(root, text=" Personnel Status", bg="#f6f6f8", font= bigFont)
PSlabel.grid(column=0, row=2, sticky="e")
PScombo = ttk.Combobox(root, value= personnelStatus, state='readonly', font= bigFont, width = 17)
PScombo.grid(column=1, row=2, sticky="w")
# Request type combo box (RT)
RTlabel = Label(root, text=" Request Type", bg="#f6f6f8", font= bigFont)
RTlabel.grid(column=0, row=3, sticky="e")
RTcombo = ttk.Combobox(root, value= requestType, state='readonly', font= bigFont, width = 17)
RTcombo.grid(column=1, row=3, sticky="w")
# Employee Name text box (EN)
ENlabel = Label(root, text=" Employee Name", bg="#f6f6f8", font= bigFont)
ENlabel.grid(column=0, row=4, sticky="e")
ENentry = Entry(root, font= bigFont, width= 19)
ENentry.grid(column=1,row=4, sticky="w")
# Employee ID text box (EID)
EIDlabel = Label(root, text=" Employee ID", bg="#f6f6f8", font= bigFont)
EIDlabel.grid(column=0, row=5, sticky="e")
EIDentry = Entry(root, font= bigFont, width= 19)
EIDentry.grid(column=1,row=5, sticky="w")
# Employee Type combo box (ET)
ETlabel = Label(root, text=" Employee Type", bg="#f6f6f8", font= bigFont)
ETlabel.grid(column=0, row=6, sticky="e")
ETcombo = ttk.Combobox(root, value= employeeType, state='readonly', font= bigFont, width = 17)
ETcombo.grid(column=1, row=6, sticky="w")
# Organization Name text box (ON)
ONlabel = Label(root, text=" Organization Name", bg="#f6f6f8", font= bigFont)
ONlabel.grid(column=0, row=7, sticky="e")
ONentry = Entry(root, font= bigFont, width= 19)
ONentry.grid(column=1,row=7, sticky="w")
# Requested For text box (RF)
RFlabel = Label(root, text=" Requested For", bg="#f6f6f8", font= bigFont)
RFlabel.grid(column=0, row=8, sticky="e")
RFentry = Entry(root, font= bigFont, width= 19)
RFentry.grid(column=1,row=8, sticky="w")
# email text box (EM)
EMlabel = Label(root, text=" Email", bg="#f6f6f8", font= bigFont)
EMlabel.grid(column=0, row=9, sticky="e")
EMentry = Entry(root, font= bigFont, width= 19)
EMentry.grid(column=1,row=9, sticky="w")
# Access Change list box (AC)
AClabel = Label(root, text=" Access Change", bg="#f6f6f8", font= bigFont)
AClabel.grid(column=0, row=10, sticky="e")
AClist = Listbox(root, selectmode=tk.MULTIPLE, height= 2, font= bigFont, width= 19)
for item in accessChange:
AClist.insert(tk.END, item)
AClist.grid(column=1, row=10, sticky="w")
# COSD ID text box (EN)
cosdIDlabel = Label(root, text=" CoSD ID", bg="#f6f6f8", font= bigFont)
cosdIDlabel.grid(column=0, row=11, sticky="e")
cosdIDentry = Entry(root, font= bigFont, width= 19)
cosdIDentry.grid(column=1,row=11, sticky="w")
# Supp F file
def openFile():
global filePath
filePath = filedialog.askopenfilename(title="Select a file")
if filePath:
fileLabel.config(text=filePath)
return filePath
imagePath = "C:\\Users\\E100676\\Pictures\\attach-button.png"
img = PhotoImage(file= imagePath)
fileButton = Button(root, image= img, command=openFile, bg="#f6f6f8", font= bigFont)
fileButton.grid(column=0,row=12, sticky="e")
fileLabel = Label(root, text="No file attached", bg="#f6f6f8", font= bigFont)
fileLabel.grid(column=1,row=12, sticky="w")
# Retrieve selections
def PSfunction(event):
global PSselection
PSselection = PScombo.get()
def RTfunction(event):
global RTselection
RTselection = RTcombo.get()
def ENfunction(event):
global ENentry
ENentry = ENentry.get()
def EIDfunction(event):
global EIDentry
EIDentry = EIDentry.get()
def ETfunction(event):
global ETselection
ETselection = ETcombo.get()
def ONfunction(event):
global ONentry
ONentry = ONentry.get()
def RFfunction(event):
global RFentry
RFentry = RFentry.get()
def EMfunction(event):
global EMentry
EMentry = EMentry.get()
def ACfunction(event):
global ACselection
selected = AClist.curselection()
ACselection = [AClist.get(index) for index in selected]
def cosdIDfunction(event):
global cosdIDentry
cosdIDentry = cosdIDentry.get()
def FLfunction(event):
global fileLabel
fileLabel = fileLabel.get()
# Bind selection
PScombo.bind("<<ComboboxSelected>>", PSfunction)
RTcombo.bind("<<ComboboxSelected>>", RTfunction)
ENentry.bind("<FocusOut>", ENfunction)
EIDentry.bind("<FocusOut>", EIDfunction)
ETcombo.bind("<<ComboboxSelected>>", ETfunction)
ONentry.bind("<FocusOut>",ONfunction)
RFentry.bind("<FocusOut>", RFfunction)
EMentry.bind("<FocusOut>", EMfunction)
AClist.bind("<<ListboxSelect>>", ACfunction)
cosdIDentry.bind("<FocusOut>", cosdIDfunction)
fileButton.bind("<Button-1>", FLfunction)
print(fileLabel)
# Create AutoFill Button
data = {
'PersonnelStatus': '',
'RequestType': '',
'EmployeeName':'',
'EmployeeID':'',
'EmployeeType':'',
'OrganizationName':'',
'RequestedFor':'',
'Email':'',
'AccessChange':[],
'CosdID': '',
'filePath' : ''
}
def closeWindow():
data['PersonnelStatus']: PSselection
data['RequestType']: RTselection
data['EmployeeName']:ENentry
data['EmployeeID']:EIDentry
data['EmployeeType']:ETselection
data['OrganizationName']:ONentry
data['RequestedFor']:RFentry
data['Email']:EMentry
data['AccessChange']:ACselection
data['CosdID']: cosdIDentry
data['filePath']: filePath
with open('data.pickle', 'wb') as file:
pickle.dump(data,file)
root.destroy()
# autoFillButton
boldFont = font.Font(family="Inherit", size=12, weight="bold")
autoFillButton = Button(root, text="Auto Fill", command=closeWindow, padx=50, pady=10, bg='#428bca', highlightbackground="#428bca",
fg='white', font= boldFont, relief=tk.FLAT)
autoFillButton.grid(row=13, column=1, sticky="w")
# Main Loop
root.mainloop()
答: 暂无答案
评论
filePath
data['filePath']: filePath
data['filePath'] = filePath