if save_file_dialog.ShowModal() == wx.ID_OK: save_path = save_file_dialog.GetPath() print(f"保存的文件路径: {save_path}") save_file_dialog.Destroy() 四、总结 通过以上三种方法,您可以轻松地在Python中创建打开和保存对话框。Tkinter库适合新手使用,PyQt库提供了更多的控件和功能,而wxPython库则...
importtkinterastkfromtkinterimportfiledialogdefopen_file():filepath=filedialog.askopenfilename()iffilepath:print(f"选择的文件是:{filepath}")root=tk.Tk()root.title("文件选择示例")open_button=tk.Button(root,text="打开文件",command=open_file)open_button.pack(pady=20)root.mainloop() 1. 2. 3...
导入模块:我们导入tkinter和filedialog,这两个模块分别用于创建窗口和文件选择对话框。 定义保存文件函数:save_file函数弹出文件保存对话框,用户可以选择文件名和路径,并写入简单的文本内容。 创建主窗口:main函数创建主窗口,并添加一个按钮,用户点击后会调用save_file函数。 运行应用程序:通过mainloop()使窗口持续显示,直...
1fromtkinterimport*23root =Tk()4#第1步,创建并添加 Canvas 组件5cv = Canvas(root, background='white')6cv.pack(fill=BOTH, expand=YES)7#第2步,调用该组件的方法来绘制图形8cv.create_rectangle(30, 30, 200, 200,9outline='red',#边框颜色10stipple='question',#填充的位图11fill='red',#填充...
3. Prompt the User for File Location and Name When the user clicks the “Save” button, we want to prompt them to choose a file location and provide a name for the file. Tkinter provides a convenient file dialogasksaveasfilename()that allows us to achieve this. Here’s an example of...
import tkinter as tk from tkinter importfiledialog, dialog window =tk.Tk() window.title('文件读取与保存') window.geometry('600x600') file_path = '' file_text = '' text1 = tk.Text(window, width=50,height=12,bg='white', font=("微软雅黑",16)) ...
importtkinter as tk fromtkinterimportfiledialog root=tk.Tk() path=filedialog.asksaveasfile(initialdir="/", title="Save file", filetypes=(("txt files","*.txt"),("all files","*.*"))) root.mainloop() For instance, you may navigate to the Desktop and save a file there with the name...
pip install tkinter openpyxl python-docx 2、创建GUI界面 我们将使用Tkinter模块来创建用户界面。以下是创建主窗口和调用文件类型选择对话框的代码: importtkinter as tkfromtkinterimportsimpledialogfromtkinterimportmessageboxfromopenpyxlimportWorkbookfromdocximportDocument#定义创建文件的函数...defshow_file_dialog(): ...
**打开一个文件:**askopenfilename() **打开一组文件:**askopenfilenames() **保存文件:**asksaveasfilename() 首先是打开一个文件我们将使用的对话函数是askopenfilename() 代码语言:javascript 代码运行次数: from tkinterimport*importtkinter.filedialog ...
tkinter.messagebox.showinfo('点击', 'OK') 参数1为消息标题。 参数2为消息内容。 放入按钮点击事件中 def btn_click(): tkinter.messagebox.showinfo('点击', 'OK') 多行文本框:Text、ScrolledText。 text = tk.Text(root) text.pack() text.insert(tk.INSERT, "123\n") # 在光标出插入 ...