Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import filedialog
- import random
- def generate_random_number():
- random_number = random.randint(0, 2**33)**(1/33)
- return random_number
- def open_file():
- file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
- if file_path:
- with open(file_path, 'r') as file:
- file_content = file.read()
- text_editor.delete("1.0", tk.END)
- text_editor.insert(tk.END, file_content)
- def save_file():
- file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
- if file_path:
- file_content = text_editor.get("1.0", tk.END)
- with open(file_path, 'w') as file:
- file.write(file_content)
- def generate_random_number_and_insert():
- random_number = generate_random_number()
- text_editor.insert(tk.END, str(random_number))
- root = tk.Tk()
- root.title("File Editor with RNG")
- # Text editor
- text_editor = tk.Text(root)
- text_editor.pack()
- # Menu bar
- menu_bar = tk.Menu(root)
- root.config(menu=menu_bar)
- # File menu
- file_menu = tk.Menu(menu_bar, tearoff=False)
- menu_bar.add_cascade(label="File", menu=file_menu)
- file_menu.add_command(label="Open", command=open_file)
- file_menu.add_command(label="Save", command=save_file)
- file_menu.add_separator()
- file_menu.add_command(label="Exit", command=root.quit)
- # Random number menu
- random_number_menu = tk.Menu(menu_bar, tearoff=False)
- menu_bar.add_cascade(label="Random Number", menu=random_number_menu)
- random_number_menu.add_command(label="Generate and Insert", command=generate_random_number_and_insert)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement