Search code examples
pythontkintercustomtkinter

CustomTkinter toplevel window widget problem


When I run the code below, I want customtkinter to create a new window , which display a label widget , whose attributes include text and font , however label widget is cut off , making the text on either end to be invisible resulting in a weird label , how do I avoid this

import tkinter as tk
import customtkinter as ctk

class FirstWindow(ctk.CTk):
    def onclick(self):
        user_info = self.entry.get()  # Get the value from the entry field
        self.second_window = SecondWindow(self, user_info)

    def __init__(self):
        super().__init__()

        self.title("User Information")
        self.geometry('600x400')
        self.resizable(False, False)

        self.entry = ctk.CTkEntry(self)
        self.entry.pack()

        button = ctk.CTkButton(self, text="Hello", command=self.onclick)
        button.pack()

class SecondWindow(ctk.CTkToplevel):
    def __init__(self, parent, user_info):
        super().__init__(parent)
        
        self.title("BMI Result") 
        self.resizable(False, False)
        self.geometry('400x200')
        fit_goal = 'hello'
        self.columnconfigure((0, 1, 2), weight=1, uniform='a')
        self.rowconfigure((0, 1), weight=1, uniform='a')

        
        label_reco = ctk.CTkLabel(self, text=f'Recommended: {fit_goal}', font=("JokerMan", 20))
        label_reco.grid(row=0, column=1)

        

    

if __name__ == "__main__":
    a = FirstWindow()
    a.mainloop()

I tried the code I mentioned above , however It displays a weird label widget which is not fully , visible , I would like the widget to be displayed fully.(I have used the jokerman font to exaggerate the effect)


Solution

  • The problem is your column definition and the locked windows size.

    This line is telling tkinter to create the first 3 columns in a uniform with:

    self.columnconfigure((0,1, 2), weight=1, uniform='a')
    

    So the wider your second column becomes (where you have your text atm), the more space the whole table uses. As the width of the window is fixed, the cells go to their max width and distribute evenly over the width of the window.

    You could use self.minsize(400, 200) instead of self.geometry('400x200') This way more space can be used if needed.

    btw: If you want to have your popup on top you could use self.attributes('-topmost', True)

    import customtkinter as ctk
    
    
    class FirstWindow(ctk.CTk):
        def __init__(self):
            super().__init__()
    
            self.title("User Information")
            self.geometry('600x400')
            self.resizable(False, False)
    
            self.entry = ctk.CTkEntry(self)
            self.entry.pack()
    
            button = ctk.CTkButton(self, text="Hello", command=self.onclick)
            button.pack()
    
        def onclick(self):
            user_info = self.entry.get()  # Get the value from the entry field
            self.second_window = SecondWindow(self, user_info)
    
    class SecondWindow(ctk.CTkToplevel):
        def __init__(self, parent, user_info):
            super().__init__(parent)
    
            self.title("BMI Result")
            self.resizable(False, False)
            # self.geometry('400x200')
            self.minsize(400, 200)
            fit_goal = 'hello'
            self.columnconfigure((0, 1, 2), weight=1, uniform='a')
            self.rowconfigure((0, 1), weight=1, uniform='a')
    
            label_reco = ctk.CTkLabel(self, text=f'Recommended: {fit_goal}', font=("JokerMan", 20))
            label_reco.grid(row=0, column=1)
            self.attributes('-topmost', True)
    
    if __name__ == "__main__":
        a = FirstWindow()
        a.mainloop()