0

Is there a function out their in python that you will declare a target i.e. a text box on another open window, and it would print text to that screen? Thanks

2
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by tondeuse34
0

So you want a text box in one window, and what you type in the text box will also show up in another window?

0

sort of, but for example if you had a open notepad it would print "Hello World!" onto that notepad

0

So two text boxes, and the text from one goes into the other?

(or do you mean MS Notepad? That would be quite a challenge).

Here's a small example:

import Tkinter as tk

class MyFrame(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.text1 = tk.Text(self, width=40, height=10)
        self.text2 = tk.Text(self, width=40, height=10, state=tk.DISABLED)
        self.text1.grid()
        self.text2.grid()

        self.text1.bind("<Any-KeyPress>", self.copytext)

    def copytext(self, event):
        text = self.text1.get("0.0", tk.END)
        self.text2["state"]=tk.NORMAL
        self.text2.delete("0.0", tk.END)
        self.text2.insert("0.0", text)
        self.text2["state"]=tk.DISABLED

app = tk.Tk()
app.frame = MyFrame(app)
app.frame.grid()
app.mainloop()

It's not perfect -- the second Text widget is one character behind the first because the <Any-KeyPress> event is processed prior to the key being inserted into the first Text.

But it gives a decent idea.

Jeff

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.