0

I made a program that swaps character in a string according my data dictionary but I want to display the swapped string as a text in the GUI that i can copy and paste outside the program. I just started using tkinter a few days ago, but i do understand python:

import tkinter as tk

root = tk.Tk()
root.title("code converter")

itext = tk.StringVar()

top_spacer = tk.Label(root, text=" ")
top_spacer.grid(row=0, column=1)

text_label = tk.Label(root, text="type/paste text you want to convert")
text_label.grid(row=0, column=2)

text_entry = tk.Entry(root, textvariable=itext)#itext is the input text
text_entry.grid(row=1, column=2)

def translate():
    trans_dict = {
        'a':'z','b':'y', 'c':'x', 'd':'w', 'e':'v', 'f':'u', 'g':'t', 'h':'s', 'i':'r', 'j':'q', 'k':'p', 'l':'o', 'm':'n'
        ,'n':'m','o':'l', 'p':'k','q':'j','r':'i','s':'h','t':'g' ,'u':'f','v':'e','w':'d','x':'c','y':'b','z':'a'
        ,'A':'Z','B':'Y', 'C':'X','D':'W','E':'V', 'F':'U','G':'T','H':'S', 'I':'R', 'J':'Q', 'K':'P', 'L':'O', 'M':'N'
        ,'N':'M','O':'L', 'P':'K','Q':'J','R':'I','S':'H','T':'G','U':'F','V':'E','W':'D','X':'C','Y':'B','Z':'A'
        #,'(1)':'(26)','(2)':'(25)','(3)':'(24)','(4)':'(23)','(5)':'(22)','(6)':'(21)','(7)':'(20)','(8)':'(19)', '(9)':'(18)', '(10)':'(17)', '(11)':'(16)', '(12)':'(15)', '(13)':'(14)'
        #,'(14)':'(13)','(15)':'(12)','(16)':'(11)','(17)':'(10)','(18)':'(9)','(19)':'(8)','(20)':'(7)','(21)':'(6)','(22)':'(5)','(23)':'(4)','(24)':'(3)','(25)':'(2)','(26)':'(1)'
                  }

    ttext = itext.get()

    translation = "".join([trans_dict.get(c,c) for c in ttext])

    ntext.set(translation)

convertion_button = tk.Button(root, text="convert text", command=translate)
convertion_button.grid(row=2, column=2)

ntext = tk.StringVar()
ntext_Message = tk.Message(root, textvariable=ntext, relief="sunken", bg="white", width=200)
ntext_Message.grid(row=3, column=2)

copy_button = tk.Button(root, text="copy text to clipboard")
copy_button.grid(row=2, column=1)

root.mainloop()

i want to display the ntext variable string in the GUI and copy it to the system clipboard when the copy button is pressed. is that possible?

  • Does this answer your question? [How do I copy a string to the clipboard on Windows using Python?](https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python) – Henry Yik Nov 01 '19 at 11:36
  • I found a way thanks – Ulectricx Nov 02 '19 at 01:24

0 Answers0