Advertisement
A_GUES

Python 3 Good Tool file

May 19th, 2023 (edited)
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import filedialog
  3. import random
  4.  
  5. def generate_random_number():
  6.     random_number = random.randint(0, 2**33)**(1/33)
  7.     return random_number
  8.  
  9. def open_file():
  10.     file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
  11.     if file_path:
  12.         with open(file_path, 'r') as file:
  13.             file_content = file.read()
  14.             text_editor.delete("1.0", tk.END)
  15.             text_editor.insert(tk.END, file_content)
  16.  
  17. def save_file():
  18.     file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
  19.     if file_path:
  20.         file_content = text_editor.get("1.0", tk.END)
  21.         with open(file_path, 'w') as file:
  22.             file.write(file_content)
  23.  
  24. def generate_random_number_and_insert():
  25.     random_number = generate_random_number()
  26.     text_editor.insert(tk.END, str(random_number))
  27.  
  28. root = tk.Tk()
  29. root.title("File Editor with RNG")
  30.  
  31. # Text editor
  32. text_editor = tk.Text(root)
  33. text_editor.pack()
  34.  
  35. # Menu bar
  36. menu_bar = tk.Menu(root)
  37. root.config(menu=menu_bar)
  38.  
  39. # File menu
  40. file_menu = tk.Menu(menu_bar, tearoff=False)
  41. menu_bar.add_cascade(label="File", menu=file_menu)
  42. file_menu.add_command(label="Open", command=open_file)
  43. file_menu.add_command(label="Save", command=save_file)
  44. file_menu.add_separator()
  45. file_menu.add_command(label="Exit", command=root.quit)
  46.  
  47. # Random number menu
  48. random_number_menu = tk.Menu(menu_bar, tearoff=False)
  49. menu_bar.add_cascade(label="Random Number", menu=random_number_menu)
  50. random_number_menu.add_command(label="Generate and Insert", command=generate_random_number_and_insert)
  51.  
  52. root.mainloop()
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement