Discussion:
[Tkinter-discuss] How to insert a string into a Text using Button
Alexander Matyukhin
2012-06-01 20:50:39 UTC
Permalink
Hello to all! I have a simple code, which use tkinter.
It has a Text widget and two Buttons. First Button
must insert string into text. But it dont do it.
The string is inserted into text after start and the
button don't react on click.
Second button must close window and it do it normal.
Where did I have an error?

Code:

#!//usr/bin/env python3.2
#_*_coding: utf-8_*_

import tkinter as tk

def InsertToText ():
text_widget.insert (1.0, 'Hello World!')

# New window
main_window = tk.Tk()

# Text widget
text_widget = tk.Text (main_window)
text_widget.pack()

# First button
button_insert = tk.Button (main_window,
text='Insert string', command=InsertToText())

button_insert.pack()

# Second button
button_close = tk.Button (main_window,
text='Close', command=main_window.destroy)

button_close.pack()

main_window.mainloop()
--
Alexander Matyukhin
Bryan Oakley
2012-06-01 21:19:34 UTC
Permalink
On Fri, Jun 1, 2012 at 3:50 PM, Alexander Matyukhin <
Post by Alexander Matyukhin
Hello to all! I have a simple code, which use tkinter.
It has a Text widget and two Buttons. First Button
must insert string into text. But it dont do it.
The string is inserted into text after start and the
button don't react on click.
Second button must close window and it do it normal.
Where did I have an error?
<snip>
# First button
button_insert = tk.Button (main_window,
text='Insert string', command=InsertToText())
Remove the () for the command attribute. It should be:

... command=InsertToText)

The way you have it, you are calling InsertToText(), and the result of that
call (None) is what is getting assigned to the command attribute of the
button.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20120601/0b96c67f/attachment.html>
Loading...