[Tkinter] text edition in Text - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] text edition in Text (/thread-34548.html) |
text edition in Text - crook79 - Aug-08-2021 Hello At the beginning I want show you what I want achieve. I using tkinter: 2 text field and 1 button: in the first text field I enter: one two three I press the button The second field should show: a:one a:two a:three but unfortunately it appears: a:one two three it is my code: import tkinter as tk from tkinter import * def OpenWindow(): root = tk.Tk() root.geometry('400x250') return root root = OpenWindow() text1 = Text(root, width='12', height='10') text1.place(x=20,y=20) text2 = Text(root, width="12", height="10") text2.place(x=240,y=20) def click(): text_input = text1.get("1.0", END) text2.delete(1.0, END) for i in text_input: row_word = ('a:' + text_input) text2.insert(1.0, row_word) button_click = Button(root, text='add', command=click) button_click.pack() root.mainloop()Thank you for help, I beginning my adventure with Python so maybe my question is tryvial... RE: text edition in Text - deanhystad - Aug-08-2021 Your problem is test_input is one string, not three strings. Your function will have to split test_input into three strings, prepend "a:" to each string, and put the strings back together. You can use str.split() for this if you know the delimiter and the delimiter is only used to separate words in the string. This is an example where words are separated by a space. words = ('one two three').split(' ') # Split string into words separated by a space row_words = [f'a:{word}' for word in words] # Prepend a: to each word print(' '.join(row_words)) # Join the words together separated by a space I think you will want to use newline ('\n') as the separator, and you might need to strip any trailing newline before splitting into words.def click(): text = text1.get("1.0", END).strip().split('\n') text2.insert(1.0, '\n'.join([f'a:{word}' for word in text])) |