Python Forum
[Tkinter] Get text entry in frame1, echo it in frame2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Get text entry in frame1, echo it in frame2
#1
I'm trying to learn tkinter.

In my window I have 2 frames, frame1 and frame2. Everything displays OK.

I can get a text entry in frame1 and I can echo it in frame1 by clicking this button.

button1 = tkinter.Button(text='Get the text message', command=getInput, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
    canvas1.create_window(200, 110, window=button1)
frame2 contains a scrolled text widget.

I would like to echo the content of entry1 from frame1 in the text widget of frame2.

How do I do that?

entry1 = tkinter.Entry (frame1) 
    canvas1.create_window(200, 70, window=entry1)

    def getInput():
        global the_id        
        canvas1.delete(the_id)
        global msg
        msg = entry1.get()        
        label3 = tkinter.Label(frame1, text= 'You entered this text:',font=('helvetica', 10))
        canvas1.create_window(200, 140, window=label3)
        # wraplength is in pixels not characters!!        
        label4 = tkinter.Label(frame1, text=msg,font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)        
        the_id = canvas1.create_window(200, 170, window=label4, tags="label")
I set msg as global
Quote:global msg
and I tried this below. I do not get any errors, but neither do I get any output in frame2:

text = scrolledtext.ScrolledText(frame2, width=40, height=10, bd=5, undo=True)
    text['font'] = ('consolas', '12')
    msg = entry1.get()  
    text.insert('1.end', msg + '\n')
    #txt.pack(expand=True, fill='both')
    text.pack(expand=False)
Seems like I can't reach from frame2 to a variable in frame1. How to shunt it over?
Reply
#2
Make it easier on us, please submit full code.
Reply
#3
Full code:

def myApp():
    window = tkinter.Tk()
    window.title("Get a text input and echo it")
    window.config(bg='light blue')
    window.geometry('640x640')

    global msg

    frame1=Frame(window,width=500,height=400, bg='green')
    #frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)
    frame1.pack(expand=False)

    frame2=Frame(window,width=500,height=400, bg='green')
    #frame.pack(expand=True, fill=BOTH) #.grid(row=0,column=0)
    frame2.pack(expand=False)

    canvas1 = tkinter.Canvas(frame1, bg='white', width = 400, height = 200,  relief = 'raised')
    #canvas1.config(xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
    canvas1.pack(padx=10, pady=10)
    
    label1 = tkinter.Label(canvas1, text='Echo a text input')
    label1.config(font=('helvetica', 14))
    canvas1.create_window(200, 10, window=label1)

    label2 = tkinter.Label(frame1, text='Type your text:')
    label2.config(font=('helvetica', 10))
    canvas1.create_window(200, 40, window=label2)

    entry1 = tkinter.Entry (frame1) 
    canvas1.create_window(200, 70, window=entry1)

    def getInput():
        global the_id        
        canvas1.delete(the_id)
        global msg
        msg = entry1.get()        
        label3 = tkinter.Label(frame1, text= 'You entered this text:',font=('helvetica', 10))
        canvas1.create_window(200, 140, window=label3)
        # wraplength is in pixels not characters!!        
        label4 = tkinter.Label(frame1, text=msg,font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)        
        the_id = canvas1.create_window(200, 170, window=label4, tags="label")
        # don't set the y value too high or you won't see the output!
        #canvas1.create_window(200, 320, window=label4)

    global the_id
    #global the_id
    label4 = tkinter.Label(frame1, text= 'Your input will be here after you click the button.',font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
    #the_id = tkinter.Label(frame, text= 'Your input will be here after you click the button.',font=('helvetica', 10, 'bold'), anchor='w', wraplength=390)
    the_id = canvas1.create_window(200, 170, window=label4, tags="label")    
    button1 = tkinter.Button(text='Get the text message', command=getInput, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
    canvas1.create_window(200, 110, window=button1)

    text = scrolledtext.ScrolledText(frame2, width=40, height=10, bd=5, undo=True)
    text['font'] = ('consolas', '12')
    global msg
    msg = entry1.get()  
    text.insert('1.end', msg + '\n')
    #txt.pack(expand=True, fill='both')
    text.pack(expand=False)
   
    window.mainloop()
Reply
#4
This code is riddled with errors.

you shouldn't use globals, there's no need for them.
with tkinter, you should use classes, forward referencing will no longer be a problem.
Here's a basic tkinter application that's done with classes: https://python-forum.io/Thread-GUI-list-...ht=tkinter
Reply
#5
Thanks, trying your code, but these 2 both fail:

pip3 install tkinter.tix
pip3 install tkinter.filedialog

This is the error I get in Idle:
Quote:Traceback (most recent call last):
File "<pyshell#11>", line 2, in <module>
root = tk.Tk()
File "/usr/lib/python3.6/tkinter/tix.py", line 214, in __init__
self.tk.eval('package require Tix')
_tkinter.TclError: can't find package Tix

pip3 install Tix also fails
Reply
#6
Not sure what problem you are having with tkinter.filedialog or tkinter.tix. Check for a spelling or another case error. Both are part of the standard libraries and you should not have to install them. What platform are you using? What version of Python?
Reply
#7
I use Ubuntu 18.04

You are right, in that

import tkinter.tix as tk
import tkinter.filedialog as tf
import pip
Do not show any errors.

But when I try and run Larz60+ code in Idle, I still get:

Quote:Traceback (most recent call last):
File "<pyshell#5>", line 2, in <module>
root = tk.Tk()
File "/usr/lib/python3.6/tkinter/tix.py", line 214, in __init__
self.tk.eval('package require Tix')
_tkinter.TclError: can't find package Tix

What is Tix?

I finally found the tkinter docs. I will buy the book and work through them. Maybe I won't have so many problems after that! (said he, hopefully but uncertain)
Reply
#8
Is this what you are trying to do?

#! /usr/bin/env python3

# Do the imports
import tkinter as tk
from functools import partial

root = tk.Tk()
root['padx'] = 20
root['pady'] = 10


def printit(text):
    text_area.delete(1.0, tk.END)
    text_area.insert(tk.END, text.get())

frame = tk.Frame(root)
frame.grid(column=0, row=0, sticky='nw', padx=10)

frame2 = tk.Frame(root)
frame2.grid(column=1, row=0, sticky='nw', padx=10)

entry = tk.Entry(frame, width=40)
entry.focus()
entry.grid(column=0, row=0, sticky='n')

text_area = tk.Text(frame2, width=40, height=10)
text_area.grid(column=0, row=0)

btn = tk.Button(root, text='Submit', command=partial(printit, entry))
btn.grid(columnspan=2, column=0, row=2, sticky='w')

root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
Thank you very much, that's great! Worked first time!

html is just a text file. Python is great for dealing with text files.

What I eventually want to do is put all my little 'makeHTML' programs in a GUI.

I have about 16 little programs for creating html, inserting media files, checkboxes, textboxes, dropdownboxes, I run them in bash. Each one requires some input from me and each one pumps out some text, so I can see what it's doing. They all work well and do just what I want.

So, eventually, I will have a GUI with 16 buttons, or a menu with 16 items, click one and off it goes. They all append their output to the same file.

This is all just for homework webpages. I also use Python to make the php file that gets the students' answers and writes them to my webpage. Python really saves me a lot of time!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get selected text inside an Entry Jerdup 0 2,443 Aug-20-2020, 12:42 PM
Last Post: Jerdup
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,295 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  [Tkinter] finding lines from a text entry Sutsro 2 1,891 May-04-2020, 09:19 AM
Last Post: Sutsro
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,420 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] getting Entry text Stauricus 4 2,968 May-10-2019, 08:24 PM
Last Post: swanysto
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,453 Mar-18-2019, 05:36 PM
Last Post: woooee
  [WxPython] bind label and entry text with return key metulburr 1 3,212 Aug-14-2018, 10:02 PM
Last Post: metulburr
  [Tkinter] Hide text in entry box when i click on it. MeeranRizvi 1 11,050 Jan-25-2017, 10:38 AM
Last Post: MeeranRizvi
  [Tkinter] Entry widget : unable to get the text entered dlemper 2 18,131 Oct-11-2016, 12:32 AM
Last Post: dlemper

Forum Jump:

User Panel Messages

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