鱼C论坛

 找回密码
 立即注册
查看: 2148|回复: 2

[已解决]有谁能用清晰奇特的思路来解决这道题

[复制链接]
发表于 2023-7-30 22:07:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
提供一个文件夹浏览框,让用户选择需要打开的文本文件,打开并显示文件内容。
当用户点击“OK”按钮的时候,比较当前文件是否修改过,如果修改过,则提示“覆盖保存”、”放弃保存”或“另存为…”并实现相应的功能。
最佳答案
2023-7-30 22:32:55
以下是一个使用Python Tkinter库实现的简单的文件浏览和文件内容显示程序:
  1. import tkinter as tk  
  2. from tkinter import filedialog  
  3.   
  4. def open_file():  
  5.     file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])  
  6.     if file_path:  
  7.         with open(file_path, "r") as file:  
  8.             content.delete(1.0, tk.END)  
  9.             content.insert(1.0, file.read())  
  10.   
  11. def save_file():  
  12.     if file_path:  
  13.         with open(file_path, "w") as file:  
  14.             file.write(content.get(1.0, tk.END))  
  15.   
  16. def check_modified():  
  17.     if file_path and file_has_been_modified:  
  18.         choice = tk.messagebox.askquestion("File modified", "The file has been modified. Do you want to save your changes?")  
  19.         if choice == "yes":  
  20.             save_file()  
  21.         elif choice == "no":  
  22.             pass  
  23.         else:  
  24.             file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", " *.txt")])  
  25.             if file_path:  
  26.                 with open(file_path, "w") as file:  
  27.                     file.write(content.get(1.0, tk.END))  
  28.   
  29. root = tk.Tk()  
  30.   
  31. file_path = None  
  32. file_has_been_modified = False  
  33.   
  34. label = tk.Label(root, text="Select a text file to open:")  
  35. label.pack()  
  36.   
  37. button = tk.Button(root, text="Open", command=open_file)  
  38. button.pack()  
  39.   
  40. content = tk.Text(root)  
  41. content.pack()  
  42.   
  43. label = tk.Label(root, text="")  
  44. label.pack()  
  45.   
  46. button = tk.Button(root, text="Save", command=save_file)  
  47. button.pack()  
  48.   
  49. label = tk.Label(root, text="")  
  50. label.pack()  
  51.   
  52. button = tk.Button(root, text="Exit", command=root.quit)  
  53. button.pack()  
  54.   
  55. root.mainloop()
复制代码

当用户点击“Open”按钮时,会弹出文件选择对话框,用户可以选择一个文本文件。文件选择后,文件内容会被读取并显示在文本框中。

当用户修改文本内容并点击“Save”按钮时,程序会检查文件是否已经被修改。如果文件已经被修改,程序会弹出一个对话框,询问用户是否要覆盖保存、放弃保存或者另存为。如果用户选择覆盖保存,程序会将文本框中的内容写入到原始文件中。如果用户选择放弃保存,程序会什么也不做。如果用户选择另存为,程序会弹出一个文件保存对话框,用户可以选择另存的文件名和路径。

当用户点击“Exit”按钮时,程序会退出。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-30 22:15:16 | 显示全部楼层
  1. import tkinter as tk
  2. import tkinter.filedialog as fd
  3. root = tk.Tk()
  4. root.withdraw()  # 隐藏窗口
  5. f = fd.askopenfilename(defaultextension=".txt", filetypes=[("文本文件", "*.txt")])
  6. f = open(f, "r")
  7. print(f)
  8. f.close()
  9. root.destory()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-30 22:32:55 | 显示全部楼层    本楼为最佳答案   
以下是一个使用Python Tkinter库实现的简单的文件浏览和文件内容显示程序:
  1. import tkinter as tk  
  2. from tkinter import filedialog  
  3.   
  4. def open_file():  
  5.     file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])  
  6.     if file_path:  
  7.         with open(file_path, "r") as file:  
  8.             content.delete(1.0, tk.END)  
  9.             content.insert(1.0, file.read())  
  10.   
  11. def save_file():  
  12.     if file_path:  
  13.         with open(file_path, "w") as file:  
  14.             file.write(content.get(1.0, tk.END))  
  15.   
  16. def check_modified():  
  17.     if file_path and file_has_been_modified:  
  18.         choice = tk.messagebox.askquestion("File modified", "The file has been modified. Do you want to save your changes?")  
  19.         if choice == "yes":  
  20.             save_file()  
  21.         elif choice == "no":  
  22.             pass  
  23.         else:  
  24.             file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", " *.txt")])  
  25.             if file_path:  
  26.                 with open(file_path, "w") as file:  
  27.                     file.write(content.get(1.0, tk.END))  
  28.   
  29. root = tk.Tk()  
  30.   
  31. file_path = None  
  32. file_has_been_modified = False  
  33.   
  34. label = tk.Label(root, text="Select a text file to open:")  
  35. label.pack()  
  36.   
  37. button = tk.Button(root, text="Open", command=open_file)  
  38. button.pack()  
  39.   
  40. content = tk.Text(root)  
  41. content.pack()  
  42.   
  43. label = tk.Label(root, text="")  
  44. label.pack()  
  45.   
  46. button = tk.Button(root, text="Save", command=save_file)  
  47. button.pack()  
  48.   
  49. label = tk.Label(root, text="")  
  50. label.pack()  
  51.   
  52. button = tk.Button(root, text="Exit", command=root.quit)  
  53. button.pack()  
  54.   
  55. root.mainloop()
复制代码

当用户点击“Open”按钮时,会弹出文件选择对话框,用户可以选择一个文本文件。文件选择后,文件内容会被读取并显示在文本框中。

当用户修改文本内容并点击“Save”按钮时,程序会检查文件是否已经被修改。如果文件已经被修改,程序会弹出一个对话框,询问用户是否要覆盖保存、放弃保存或者另存为。如果用户选择覆盖保存,程序会将文本框中的内容写入到原始文件中。如果用户选择放弃保存,程序会什么也不做。如果用户选择另存为,程序会弹出一个文件保存对话框,用户可以选择另存的文件名和路径。

当用户点击“Exit”按钮时,程序会退出。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 07:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表