Search code examples
pythonuser-interfacetkinterpython-class

Tkinter LabelFrames don't show up


When i want to layout my frames the Labels don't show up. I can't seem to solve it. For some reason it does show op the entries that i've made. Can somebody please help me.

import tkinter as tk
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image


class main_screen():
    def __init__(self, master):
        self.master = master
        self.master.title("Roboframe")
        self.master.geometry("650x650")
        self.create_frames()
        self.create_entries()

    def create_frames(self):
        self.top = Frame(self.master).grid(row=0,column=0)
        self.bottom = Frame(self.master).grid(row=0, column=0)
        self.set_paths = LabelFrame(self.master, text="Set Path", padx=10, pady=10).grid(row=0,column=0)
        self.options = LabelFrame(self.master, text="Options", padx=10, pady=10).grid(row=0,column=0)

    def create_entries(self):
        python_path = StringVar(self.set_paths, "C:/Python37/python.exe")
        robot_path = StringVar(self.set_paths, "C:/ws/cmge.automation/RobotFrameworkCMGE")
        self.set_path_python = Entry(self.set_paths, width=60, textvariable=python_path).grid(row=0,column=0)
        self.set_path_robot = Entry(self.set_paths, width=60, textvariable=robot_path).grid(row=1, column=0)

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

Output of code shown above

The thing i'm rewritting the code for because it is a mess

The second picture i've also made myself. But the code is a giant mess.


Solution

  • You have to make an object of the widget not the grid function of the widget. Grid returns nothing so naturally none of them will show up. I believe this is what you wanted:

    import tkinter as tk
    from tkinter import *
    from tkinter import filedialog
    from PIL import ImageTk, Image
    
    
    class main_screen():
        def __init__(self, master):
            self.master = master
            self.master.title("Roboframe")
            self.master.geometry("650x650")
            self.create_frames()
            self.create_entries()
    
        def create_frames(self):
            # you have grided all of your frames and label frames on the same row and column
            self.top = Frame(self.master)
            self.top.grid(row=0,column=0)
            self.bottom = Frame(self.master)
            self.bottom.grid(row=0, column=0)
            self.set_paths = LabelFrame(self.master, text="Set Path", padx=10, pady=10)
            self.set_paths.grid(row=0,column=0)
            self.options = LabelFrame(self.master, text="Options", padx=10, pady=10)
            self.options.grid(row=0,column=0)
    
        def create_entries(self):
            python_path = StringVar(self.set_paths, "C:/Python37/python.exe")
            robot_path = StringVar(self.set_paths, "C:/ws/cmge.automation/RobotFrameworkCMGE")
            self.set_path_python = Entry(self.set_paths, width=60, textvariable=python_path)
            self.set_path_python.grid(row=0,column=0)
            self.set_path_robot = Entry(self.set_paths, width=60, textvariable=robot_path)
            self.set_path_robot.grid(row=1, column=0)
    
    root = tk.Tk()
    app = main_screen(root)
    root.mainloop()
    

    Also a couple of things:

    • You have imported tkinter twice in two different ways, just use one of them
    • You are griding both of the LabelFrames and frames on the same row and column but since "self.options" does not contain anything it is not going to show up, be careful later on