Hi. I would like to know how to get the position of the cursor (blinking line in any text control) to insert there a string.

I would also like to know how to get the value of a text widget and store it in a variable.

Recommended Answers

All 6 Replies

INSERT takes care of that, for example:

import Tkinter as tk

root = tk.Tk(className = " Text")

# text entry field, width=width chars, height=lines text
text1 = tk.Text(root, width=70, height=20)
text1.pack()

str1 = "First string"
str2 = "Second string"

text1.insert(tk.INSERT, str1)

text1.insert(tk.INSERT, str2)

root.mainloop()

To get the whole content of the Text widget and store it in a variable use:

# get the text from beginning to end
# line starts with 1 and column with 0
my_text = text1.get(1.0, tk.END)
commented: This post was very helpful, it answered the question just as planted. +2

Well, now it all works perfectly. Thanks.
New problem:
I have a function in the scripts file that requires an argument (string), So if I write it like this (in the scripts.py file)

def Function(stringvar):
     print stringvar

and I invoque it like this in the gui.py file

scripts.function(stringvar)

Then it runs automatically at the beginning of the execution, and won't run later. But, if I put it like this in the scripts.py file

def Function(event_obj, stringvar):
     print stringvar

Then it will ask me for 2 arguments!

The Python statement print is only for console programs. The extra event_obj argument does not make much sense here.

You could show short texts in the title of your GUI frame, or create a label or text widget/object to show longer texts.

I'm just saying like a sample function, but my problem is that if I put event_obj, they ask me for an extra argument, but If I don't put it, then the function will run automatically at the beginning.

What could I put in the function so that it doesn't run at the beginning, but that won't ask me for an extra argument?

Hi,
I assume, I understand your problem. When you import a module, python will compiles that script and executes for you.

#script.py
def function(strvar):
    print strvar

function('example string')
"""OUTPUT
>>>
example string
>>>"""
#test-script.py
import script
script.function('test-script.py')
""" OUTPUT
>>> 
example string
test-script.py
>>> 
"""

You can avoid this by simply commenting function call in script.py, which is an obvious and quick solution to avoid execution of the module which you import. But can you see, you have to uncomment function call when you want to individually test script.py.

There is also better method which is more useful. I am going to modify script.py little bit,

#script.py
def function(strvar):
    print strvar

if __name__ == '__main__':				# condition computes to TRUE only when this script is run and not otherwise.
		function('example string')

With this you can import module without letting Python to execute, when this script is imported, and also excute the imported module individually for testing without modifying(comment or uncomment) your code.

Hope this helps,
kath.

commented: It is the solution! +2

Nice!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.