Python Forum
[Tkinter] text edition in Text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] text edition in Text
#1
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...
Reply
#2
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
Output:
a:one a:two a:three
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]))
crook79 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Transparent window background, but not text on it muzicman0 7 2,738 Feb-02-2024, 01:28 AM
Last Post: Joically
  [Kivy] Create a function to store text fields and drop downs selection in KivyMD floxia 0 1,609 Dec-18-2022, 04:34 AM
Last Post: floxia
  [Tkinter] Updating tkinter text BliepMonster 5 5,677 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,510 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,738 Nov-04-2022, 09:04 PM
Last Post: malonn
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,450 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [PyQt] Determine whether text in QTableView cell is fully visible or not random_nick 0 957 Oct-27-2022, 09:29 PM
Last Post: random_nick
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,767 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,674 Jun-26-2022, 06:26 PM
Last Post: menator01
  [PyQt] Determining the format attributes on text in a QTextEdit object DrakeSoft 7 3,452 Apr-18-2022, 06:40 PM
Last Post: DrakeSoft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020