mardi 10 mai 2016

Trying to embed tkinter checkboxes in a text box, within a toplevel

I am working on a simple database that tracks bicycle inventory. Deep within my program I am hitting a snag that has stumped me all day. I am trying to create a list of checkboxes in a text box, (to make use of the scrollbar), all within a toplevel popup.

I can't upload my entire code, it's just too much, but here is the snippet dumbed down to get the thing working:

class Delete_bike:

    def __init__(self, master):
        self.master = master
        top = self.top = tk.Toplevel(self.master)

        text_frame = tk.Frame(self.top)
        text_frame.grid(row = 0, padx = 10, pady = 10)

        scb = tk.Scrollbar(text_frame)
        scb.grid(row = 0, column = 1, sticky = 'ns')

        d_tb = tk.Text(text_frame, height = 8, width = 40, yscrollcommand = scb.set)
        d_tb.configure(font = ('Times', 10, 'bold'))
        d_tb.grid(row = 0, column = 0)

        scb.config(command = d_tb.yview)

        test_d = {}
        for i in range(10):
            test_d[i] = 0

        for i in test_d:
            test_d[i] = tk.IntVar()
            cb = tk.Checkbutton(text = i, variable = test_d[i])
            d_tb.window_create(tk.END, window = cb)
            d_tb.insert(tk.END, '\n')

The true version makes use of drawing from a shelved dictionary to populate the extensive list of bikes.

When I run this, I get the following exception, which I do not understand at all:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__
    return self.func(*args)
  File "C:\Users\Gregory\Desktop\Bike Roster v2.0.pyw", line 115, in delete_bike
    x = Delete_bike(self.master)
  File "C:\Users\Gregory\Desktop\Bike Roster v2.0.pyw", line 239, in __init__
    d_tb.window_create(tk.END, window = cb)
  File "C:\Python34\lib\tkinter\__init__.py", line 3296, in window_create
    + self._options(cnf, kw))
_tkinter.TclError: can't embed .52341336 in .52340888.52341000.52341112

So next, I copied the snippet to a stand alone program, copied next, which works perfectly fine. So can I not achieve my desired result in a toplevel? Granted I am new at all this and have no formal training or instruction on programming, but that seems to be the only difference I can see.

import tkinter as tk
from tkinter import ttk
import tkinter.scrolledtext as tkst



class Delete_bike:

    def __init__(self, master):
        self.master = master
#        top = self.top = tk.Toplevel(self.master)

        text_frame = tk.Frame(self.master)
        text_frame.grid(row = 0, padx = 10, pady = 10)

        scb = tk.Scrollbar(text_frame)
        scb.grid(row = 0, column = 1, sticky = 'ns')

        d_tb = tk.Text(text_frame, height = 8, width = 40, yscrollcommand = scb.set)
        d_tb.configure(font = ('Times', 10, 'bold'))
        d_tb.grid(row = 0, column = 0)

        scb.config(command = d_tb.yview)

        test_d = {}
        for i in range(10):
            test_d[i] = 0

        for i in test_d:
            test_d[i] = tk.IntVar()
            cb = tk.Checkbutton(text = i, variable = test_d[i])
            d_tb.window_create(tk.END, window = cb)
            d_tb.insert(tk.END, '\n')


root = tk.Tk()
app = Delete_bike(root)

root.mainloop()

If I remove the hash-tag to activate the toplevel line of code and place the frame inside the toplevel, it generates the same error message. Left like this, it works.

And a second quick question, if I am doing something wrong here, and this can be achieved within a toplevel, can the scrolledtext module be used in lieu of the text box and scrollbar combination?

Thanks as always!




Aucun commentaire:

Enregistrer un commentaire