pbreach
pbreach

Reputation: 16997

Widgets are not showing up in simple tkinter app

I've looked at other question with a similar title and have read the answers, however nothing has worked for me. I am trying to make a simple app with a listbox + scroll bar with two buttons below it all within a group box. I've used pyqt but this is my first time using tkinter:

import tkinter as tk


class InputWindow(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.initialize()

    def initialize(self):
        # Group box to contain the widgets
        self.input = tk.LabelFrame(self, text="Input Files")

        # Listbox with scrollbar to the side
        self.listbox = tk.Listbox(self.input)
        self.scrollbar = tk.Scrollbar(self.listbox, orient=tk.VERTICAL)
        self.listbox.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.listbox.yview)
        self.listbox.grid(row=0, column=0, columnspan=2)

        self.add_btn = tk.Button(self.input, text="Add...")
        self.add_btn.grid(row=1, column=0)

        self.remove_btn = tk.Button(self.input, text="Remove")
        self.remove_btn.grid(row=1, column=1)


if __name__ == "__main__":

    root = tk.Tk()
    app = InputWindow(root)
    root.mainloop()

This is more or less what I want but in tkinter:

enter image description here

What am I doing wrong/how can this be done?

Upvotes: 0

Views: 95

Answers (1)

nbro
nbro

Reputation: 15837

You're forgetting two things:

  1. To pack (or grid or place) app
  2. To pack (or grid or place) input

You're program with the required statements:

import tkinter as tk


class InputWindow(tk.Frame):

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.initialize()

    def initialize(self):
        # Group box to contain the widgets
        self.input = tk.LabelFrame(self, text="Input Files")

        # Listbox with scrollbar to the side
        self.listbox = tk.Listbox(self.input)
        self.scrollbar = tk.Scrollbar(self.listbox, orient=tk.VERTICAL)
        self.listbox.config(yscrollcommand=self.scrollbar.set)
        self.scrollbar.config(command=self.listbox.yview)
        self.listbox.grid(row=0, column=0, columnspan=2)

        self.add_btn = tk.Button(self.input, text="Add...")
        self.add_btn.grid(row=1, column=0)

        self.remove_btn = tk.Button(self.input, text="Remove")
        self.remove_btn.grid(row=1, column=1)

        self.input.pack(expand=1, fill="both") # Do not forget to pack!

if __name__ == "__main__":

    root = tk.Tk()
    app = InputWindow(root)
    app.pack(expand=1, fill="both")  # packing!
    root.mainloop()

Upvotes: 2

Related Questions