[Tkinter-discuss] How to insert a string into a Text using Button

Alexander Matyukhin alex.matyukhin at yandex.ru
Fri Jun 1 22:50:39 CEST 2012


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



More information about the Tkinter-discuss mailing list