kytexaranort kytexaranort - 9 months ago 55
Python Question

inserting a checkbox into a text box, python 3.6

As the title says I want to have a checkbox inserted into a text box. I have tried and searched ALOT of different ways I could think of and nothing is working. Any help is greatly appreciated!
I'm not going to list every way I tried otherwise this page would be incredibly long.

The following code is just an example without the checkbox.

import tkinter as tk
self = tk.Tk()
TEXT_BOX = tk.Text(self, width = 20, height = 10)
TEXT_BOX.pack()
app = self
app.mainloop()


Also this is the checkbox I wanted to use, i just cant figure out how to insert it.

var1 = tk.IntVar()
TEST = tk.Checkbutton(self, variable = var1)


If you know of a better method please let me know.

Solved thanks to Bryan. The working code is;

import tkinter as tk
self = tk.Tk()
TEXT_BOX = tk.Text(self, width = 20, height = 10)
TEXT_BOX.pack()
var1 = tk.IntVar()
TEST = tk.Checkbutton(self, variable = var1)
TEXT_BOX.window_create("1.0", window = TEST)
TEXT_BOX.insert("end", "\n")
app = self
app.mainloop()

Answer Source

To insert a checkbutton (or any widget) into a text widget, use the documented method window_create.

Example:

TEXT_BOX.window_create("1.0", window=TEST)
TEXT_BOX.insert("end", "<-- a checkbox!\n")

This is mentioned in the text widget documentation (eg: help(tk.Text)) as well as many popular tutorials:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download