Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8712267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:04:31+00:00 2026-06-13T05:04:31+00:00

So I’ve been trying to make some basic GUIs with tkinter (not te be

  • 0

So I’ve been trying to make some basic GUIs with tkinter (not te be confused with Tkinter) and I ran into a problem for which I know no solution and can’t really find anything on the almighty Google…
I have a small SQLite database with a table of directories on my pc. I would like to draw all directorypaths into a label and add a ‘rempve’ button next to that label. The button should be able to remove directory from the database and also remove it from the GUI. I also have a ‘add’ button where one can add directories to the database and this new directory should be shown in the GUI. This is my basic layout:

---------------
| ADD         |
|dir1   REMOVE|
|dir2   REMOVE|
---------------

I use the gridlayout to show the buttons and labels. Most things work, all database related stuff works. Also when starting the GUI the current directories and ‘remove’-buttons are shown nicely. BUT… when using the ‘remove’ button the directory does not disappear from the GUI even though it is not in the database anymore, restarting the GUI fixes it of course. Adding a label works… but I’m not sure if I’m doing it correctly…
How can I somehow ‘repaint’ the GUI with the new information?
This is my code for the GUI:

class GUI():

def __init__(self,db):
    self.root = Tk()
    self.root.title("Example")
    self.frame = ttk.Frame(self.root, padding="3 3 12 12")
    self.frame.rowconfigure(5, weight=1)
    self.frame.columnconfigure(5, weight=1)
    self.frame.grid(sticky=W+E+N+S)

    lbl = ttk.Label(self.frame, text="", width=17)
    lbl.grid(row=0, column=2, sticky=W)
    ttk.Button(self.frame, text="Add directory", command=lambda:self.load_file(db), width=30).grid(row=0, column=0, sticky=W, padx=(500,50))
    ttk.Button(self.frame, text="Sort files", command=lambda:self.sort(db,lbl), width=17).grid(row=0, column=1, sticky=W)
    self.draw(db)
    self.root.mainloop()

def load_file(self,db):
    fname = filedialog.askdirectory()
    db.addPath(fname)
    self.draw(db)

def remove_dir(self,db,pid):
    db.removePath(pid)
    self.draw(db)

def sort(self,db,lbl):
    lbl['text'] = 'Sorting...'
    sortFiles.moveFiles(db)
    lbl['text'] = 'Done!'


def draw(self,db):
    i = 0
    paths = db.getPaths()
    for path in paths:
        ttk.Label(self.frame,text=path[1]).grid(row=1+i,column=0,sticky=W)
        ttk.Button(self.frame, text="Remove directory", command=lambda:self.remove_dir(db,path[0]), width=17).grid(row=1+i,column=1, sticky=E)
        i = i+1
    for child in self.frame.winfo_children(): child.grid_configure(padx=5, pady=5)
    if i == 0:
        ttk.Label(self.root,text='No directories added yet').grid(row=1,column=0,sticky=W)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:04:32+00:00Added an answer on June 13, 2026 at 5:04 am

    If you prefer to redraw the GUI every time you add or delete something, you need to first destroy any old widgets before creating new ones. For example:

    def draw(self, db):
        # first, delete any existing widgets
        for child in self.frame.winfo_children():
            child.destroy()
        # next, redraw all the widgets
        paths = db.getPaths()
    for path in paths:
            ...
    

    You have another bug, which is how you’re using lambda. As it stands with the code in the question, all of your callbacks will see the same value. By specifying the value as a keyword argument to the lambda you’ll get the right value:

    ttk.Button(..., command=lambda p=path[0]:self.remove_dir(db, p)...)
    

    Unrelated to the actual problem, I don’t think you need to be passing db around. Assuming you only use a single db, I recommend you do self.db = db in your GUI constructor. That will make your code just a little easier to maintain because your method signatures will be simplified.

    Finally, there’s really no need to completely redraw the GUI when you delete one item. You can delete just one label and button at a time. This requires that you spend a little more time thinking about how you manage data in your program. If, for example, you keep a reference to each label and button, you can delete it when you delete the path from the database. Your removeDir function might look something like:

    def removeDir(self, pid):
        label, button = self.widgets(pid)
        label.destroy()
        button.destroy()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I am currently running into a problem where an element is coming back from
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.