【问题标题】:Python GUI text editorPython GUI 文本编辑器
【发布时间】:2022-01-15 23:41:03
【问题描述】:

我有一个用 tkinter 制作的 GUI 文本编辑器。它当前打开、保存和清除屏幕。我添加了一个加密和解密按钮,最终将凯撒密码应用于屏幕上的任何文本。 对于我的加密按钮,我到目前为止有

def encrypt():
    text = editor.get(1.0, tk.END)
    encryptList = []
    for word in text:
        encryptList.append(word)
        
    random.shuffle(encryptList)
    return encryptList

我想要做的是将屏幕上的文本从我那里的任何内容中随机播放(我使用带有“测试”的测试文件打印了很多次)在我能够做到这一点之后,我可以对屏幕上的文本实施 cesar 密码。 (希望)

整个代码:

#Import Module
import random
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter import ttk


#Create Function for New File Button
def fileNew():
    editor.delete(1.0, tk.END)

#Create Function For Open File Button
def fileOpen():
    editor.delete(1.0, tk.END)
    filepath = askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])      
    with open(filepath, "r") as file:
        text = file.read()
        editor.insert(tk.END, text)
        
def encrypt():
    text = editor.get(1.0, tk.END)
    encryptList = []
    for word in text:
        encryptList.append(word)
        
    random.shuffle(encryptList)
    return encryptList
        
    
        
#Create Function For Save File Button
def fileSave():
    save = asksaveasfilename(defaultextension=".*", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
    if not save:
        return
    with open(save, "w") as fileOutput:
        text = editor.get(1.0, tk.END)
        fileOutput.write(text)
        
        
        
#GUI
root = tk.Tk()
root.rowconfigure(0, minsize=500, weight=1)
root.columnconfigure(1, minsize=800, weight=1)
#Title
root.title()



#Text Editor
editor = tk.Text(root)
editor.configure(background="#263D42")
editor.grid(row=0, column=1, sticky="nsew")



#Scrollbar
scrollbar = ttk.Scrollbar(root, orient='vertical', command=editor.yview)
scrollbar.grid(row=0, column=2, sticky='ns')



#Left Side Panel
sidePanel = tk.Frame(root)
sidePanel.configure(background="#232e3a")
sidePanel.grid(row=0, column=0, sticky="ns")


#Open File Button
btn_open = tk.Button(sidePanel, text="Open", command=fileOpen)
btn_open.configure(background="#867c91")
btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)

#New File Button
btn_new = tk.Button(sidePanel, text="New", command=fileNew)
btn_new.configure(background="#867c91")
btn_new.grid(row=1, column=0, sticky="ew", padx=5, pady=5)

#Encrypt File Button
btn_open = tk.Button(sidePanel, text="Encrypt", command=encrypt)
btn_open.configure(background="#867c91")
btn_open.grid(row=2, column=0, sticky="ew", padx=5, pady=5)

#Decrypt File Button
btn_open = tk.Button(sidePanel, text="Decrypt")
btn_open.configure(background="#867c91")
btn_open.grid(row=3, column=0, sticky="ew", padx=5, pady=5)

#Save Button
btn_saveas = tk.Button(sidePanel, text="Save As", command=fileSave)
btn_saveas.configure(background="#867c91")
btn_saveas.grid(row=4, column=0, sticky="ew", padx=5, pady=5)



root.mainloop()

【问题讨论】:

  • 你没有问过问题,你只是描述了你在做什么。你问的是什么问题?

标签: python user-interface tkinter


【解决方案1】:

关闭。我想通了

def encrypt():
    text = editor.get(1.0, tk.END)
    encryptList = []
    for word in text:
        encryptList.append(word)
        
    random.shuffle(encryptList)

需要添加

editor.delete(1.0, tk.END)
editor.insert(tk.END, encryptList)

【讨论】:

  • 您的问题可以与问题的一点解释有关,而您的答案可以与解决方案的解释有关。我们所知道的是这段代码“有效”,而问题中的代码不知何故没有“有效”。请参阅How to AskHow to Answer,了解更多关于提出好问题并妥善回答问题的信息。
猜你喜欢
  • 2021-02-12
  • 2014-02-13
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多