Galoxys
Galoxys

Reputation: 1

Tkinter button will not run command

Currently, I am working on a project that is a translator from English to Morse Code. I am using Tkinter at the moment to set up a GUI. When I open and run the GUI, I can input text into the boxes and clear it using the Clear button - but the convert button will not work, which is supposed to take text from the top text box and convert/encrypt it to the bottom text box. I am unsure how to fix my problem. This file is also taking another class from a separate file which only contains a dictionary of the conversions.

import tkinter
from MorseCodeTranslator.MorseCode import MorseCode as mc


class MorseCodeGUI(tkinter.Frame):
    def __init__(self):
        super(MorseCodeGUI, self).__init__()
        self.mc = mc()
        root.title("Morse Code Translator")
        root.geometry("400x350")

        self.field1 = tkinter.Text(root, height=5, width=25, font="arial 13")
        self.field1.pack()
        self.field2 = tkinter.Text(root, height=5, width=25, font="arial 13")
        self.field2.pack()
        self.button_convert = tkinter.Button(root, text="Convert", command=self.convertText)
        self.button_convert.pack()
        self.button_clear = tkinter.Button(root, text="Clear", command=self.clearText)
        self.button_clear.pack()

    def clearText(self):
        self.field1.delete(1.0, tkinter.END)
        self.field2.delete(1.0, tkinter.END)

    def convertText(self):
        self.message = self.field1.get(1.0, tkinter.END)[:-1]
        translate = self.encrypt(self.message)

    def encrypt(self, message):
        cipher = ''
        for letter in message:
            if letter != ' ':
                cipher += mc.MORSE_DICT[letter] + ' '
            else:
                cipher += ' '

        return cipher


root = tkinter.Tk()
gui = MorseCodeGUI()
tkinter.mainloop()

Upvotes: 0

Views: 141

Answers (1)

Keldan Chapman
Keldan Chapman

Reputation: 658

You need to actually insert the value into the text box once you have it. Try this:

def convertText(self):
    self.message = self.field1.get(1.0, tkinter.END)[:-1]
    translate = self.encrypt(self.message)
    self.field2.delete(1.0,"end")
    self.field2.insert(1.0, translate)

Upvotes: 1

Related Questions