Tkinter show me results in a Text Box (as if it were print)

Question:

I need some help, I think I’m getting confused all by myself.

I am trying to create a program that translates words or letters into other words. As code I did it perfectly:

import os
import re


Text = input("Text: ")
Text = Text.upper()

myDicc = {"AB": "Hello", "ET": "Beer", "OX": "Travel", "4BA2": "Car"}


for key, value in myDicc.items():
   Text = (re.sub(key, value, Text))
print(Text)
Texto: ET AB AB ET
Beer Hello Hello Beer

Process finished with exit code 0

So far so good, but now I’m trying to create a Tkinter to do it in program mode, and that’s when I have the problem:

import os
import re
from tkinter import *

raiz = Tk()

raiz.title("Conversor Textos")
raiz.resizable(0, 0)

miFrame = Frame()
miFrame.pack(side="bottom")

Text1 = Text(miFrame, width=40, height=15, font=("Arial", 16))
Text1.grid(row=4, column=1, padx=10, pady=10)

Text2 = Text(miFrame, width=40, height=15, font=("Arial", 16))
Text2.config(state=DISABLED)
Text2.grid(row=4, column=4, padx=10, pady=10)

myDicc = {"AB": "Hello", "ET": "Beer", "OX": "Travel", "4BA2": "Car"}


def codButton():
    for key, value in myDicc.items():
        i = (re.sub(key, value, Text1.get("1.0")))
    Text2.insert("1.0", i)


TxtButton = Button(miFrame, text="Translate", command=codButton)
TxtButton.grid(row=5, column=1, pady=20)


raiz.mainloop()

I want the first code to be included in the Tkinter so that by entering the same words that I had in the Dictionary in Text Box 1, and then hitting the Translate button, they appear in Text Box 2 (but only in read mode). And if I delete and retype something else in Text Box 1 and retype a different word, the update will appear in Text Box 2. I don’t know if I have explained myself correctly.

I hope you can help me to understand it because I am doing something wrong for sure. Thank you very much ^^

Asked By: Isidoro Plasencia

||

Answers:

I see three problems:

  1. DISABLE doesn’t allow to insert text – even using code. So you have to set it NORMAL before inserting
Text2.config(state=NORMAL)
Text2.insert("1.0", i)
Text2.config(state=DISABLED)
  1. get('1.0') gives only first char – you need get('1.0', 'end')

EDIT: as @BryanOakley noticed in comment it should be get('1.0', 'end-1c') (minus 1 char) to skip extra newline which widget adds to text.

  1. similar to first code you have to assign re.sub() to the same element to use it again in next loop – so you should first get text from widget, next use it in loop, and later put in other widget
    text = Text1.get('1.0', 'end')
    
    for key, value in myDicc.items():
        text = re.sub(key, value, text)
        
    print(text)
    Text2.config(state=NORMAL)
    Text2.insert("1.0", text)
    Text2.config(state=DISABLED)

EDIT:

Another problem: you may need .delete('1.0', 'end') to remove previous content because insert() doesn’t remove it.


My version with few other changes:

import os
import re
import tkinter as  tk  # PEP8: `import *` is not preferred

# --- functions ---  # PEP8: all functions after imports (and classes) before main code 

def translate():  # PEP8: `lower_case_names` for functions and variables
    
    text = text1.get('1.0', 'end')
    
    for key, value in my_dicc.items():
        text = re.sub(key, value, text)
        
    print(text)
    text2.config(state='normal')
    text2.delete('1.0', 'end')
    text2.insert('1.0', text)
    text2.config(state='disabled')

# --- main ---

# PEP8: `lower_case_names` for functions and variables

# - data -

my_dicc = {"AB": "Hello", "ET": "Beer", "OX": "Travel", "4BA2": "Car"}

# - gui -

root = tk.Tk()

root.title("Conversor Textos")
root.resizable(0, 0)

mi_frame = tk.Frame(root)  # it is good to use `parent` in all widgets
mi_frame.pack(side="bottom")

text1 = tk.Text(mi_frame, width=40, height=15, font=("Arial", 16))
text1.grid(row=4, column=1, padx=10, pady=10)

text2 = tk.Text(mi_frame, width=40, height=15, font=("Arial", 16))
text2.grid(row=4, column=4, padx=10, pady=10)
text2.config(state='disabled')

txt_button = tk.Button(mi_frame, text="Translate", command=translate)
txt_button.grid(row=5, column=1, pady=20)

root.mainloop()

PEP 8 — Style Guide for Python Code


Answered By: furas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.