-2

Hi I am trying to create a GUI with tkinter but I am unable to programmatically make the buttons show and/or behave properly.

This is the code I am using to create the buttons based on a a dict:

from tkinter import *
import sqlite3
from functools import partial


def query(x):
    conn = sqlite3.connect("_db")
    cur = conn.cursor()
    q = cur.execute("SELECT text, option1, option2, option3 FROM tbl_1 WHERE id=?", [x, ])
    actions = q.fetchone()
    return actions


def pkey(identifier, label):
    q = query(identifier)
    buttons = {}
    text = q[0]
    label.config(text=text)
    for entry in q:
        if entry != text and entry is not None:
            buttons[int(entry[0])] = entry[3:]
            new_button = Button(root)
        for k, v in buttons.items():
            new_button.config(text=v, command=partial(pkey, k, label))
            new_button.pack(fill=X)
    print(buttons)
    lbl.pack()


root = Tk()
root.geometry("300x200")
text_frame = LabelFrame(root, bg="#A66D4F")
text_frame.pack(fill=BOTH, expand=Y)
options_frame = LabelFrame(root, bg="cyan").pack(fill=X)
lbl = Label(text_frame)
pkey(1, lbl)
root.mainloop()

This creates the buttons within a frame, but when I click one of the buttons and they should replace the existing buttons all I get is a instance of the buttons on top of the existing one. Going from 3 buttons to 6 buttons.

Is there a way to replace the existing instance of the buttons with a new one after clicking?

Thanks

emorais
  • 1
  • 5
  • 1
    Please consider adding a code sample, or revising the one you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you; here is a [great resource](http://stackoverflow.com/help/mcve) to get you started on that. Good luck with your code! – Reblochon Masque Jan 28 '18 at 17:02
  • 1
    Please show your code. We can't diagnose code we can't see. If your code is simply putting new buttons on top, then clearly, you're not destroying the old widgets. – Bryan Oakley Jan 28 '18 at 17:09
  • You can have a narrower code still. – Nae Jan 28 '18 at 17:56
  • Possible duplicate of [Switch between two frames in tkinter](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter) – Nae Jan 28 '18 at 17:56
  • At worst, see this https://stackoverflow.com/q/48480974/7032856 – Nae Jan 28 '18 at 17:59

1 Answers1

0

@BryanOakley Thank you for the reply. You were correct I was missing a destroy. i have updated the code with the below and it is working properly now.

def destroy(frame, key, label):
    frame.destroy()
    frame = LabelFrame(self.parent, bg="cyan")
    frame.pack(fill=X)
    option_buttons(frame, key, label)

Thanks again

emorais
  • 1
  • 5