Search code examples
pythontkinterlabelpaneltkinter-scale

How to set the size of a label inside a panel in tkinter - python


I have three problems!

Problem 1: I am creating a label inside a panel and its size can be changed. I want to keep it fixed. It's possible?

If you move the cursor over the upper and lower limits of the label, you will see that the cursor changes to the screen adjustment format.

Problem 2: My button is taking up the entire dimension of the panel. How to resize it without creating an empty label below?

Problem 3: The scales also occupy the entire panel horizontally. Is it possible to change its size?

from tkinter import*
import tkinter

root = Tk()
root.geometry('900x500')


var_a = DoubleVar()
var_b = DoubleVar()
    
############# CREATING PANELS #####################
#----------- General Panel  --------------#
panel_1 = PanedWindow(bd=4,orient = HORIZONTAL ,relief="raised")#, bg = "red")
panel_1.pack(fill=BOTH, expand=1)    
#----------- Fist Panel  --------------#
panel_3 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "yellow")
panel_1.add(panel_3, minsize=200) #inserting on panel_1
#----------- Second Panel  --------------#
panel_2 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "blue")
panel_1.add(panel_2, minsize=800) #inserting on panel_1
    

label2=Label(panel_3,text="Pass the cursor below me")
panel_3.add(label2)

textbox2=Scale(panel_3,orient=HORIZONTAL,variable = var_a)
panel_3.add(textbox2)

label4=Label(panel_3,text="Pass the cursor above me too")
panel_3.add(label4)

textbox4=Scale(panel_3,orient=HORIZONTAL,variable = var_b)
panel_3.add(textbox4)

def bla():
    pass
button1 = Button(panel_3,text="Why I have this size?", height = 1, width = 1, command= bla())
panel_3.add(button1)


tkinter.mainloop()

Solution

  • Based on your problem it is clear that when you add the pack() method and the expand parameter in the panel, what is being it is that all widget containers or panels expand according to their parent container, each widget fits according to the container.

    From what I was able to infer from your problem what you can do is add the pack() method with the fill=X parameter by referencing that it only fits a horizontal direction to the button and using the widget expansion in the panel by adding the expand=1 parameter.

    Here's what I did:

    from tkinter import*
    import tkinter
    
    root = Tk()
    root.geometry('900x500')
    
    
    var_a = DoubleVar()
    var_b = DoubleVar()
        
    ############# CREATING PANELS #####################
    #----------- General Panel  --------------#
    panel_1 = PanedWindow(bd=4,orient = HORIZONTAL ,relief="raised")#, bg = "red")
    panel_1.pack(fill=BOTH, expand=1)    
    #----------- Fist Panel  --------------#
    panel_3 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "yellow")
    panel_1.add(panel_3, minsize=200) #inserting on panel_1
    #----------- Second Panel  --------------#
    panel_2 = PanedWindow(panel_1, orient = VERTICAL, relief="raised")#, bg = "blue")
    panel_1.add(panel_2, minsize=800) #inserting on panel_1
     
    
    label2=Label(panel_3,text="Pass the cursor below me")
    panel_3.add(label2)
    
    textbox2=Scale(panel_3,orient=HORIZONTAL,variable = var_a)
    panel_3.add(textbox2)
    
    label4=Label(panel_3,text="Pass the cursor above me too")
    panel_3.add(label4)
    
    textbox4=Scale(panel_3,orient=HORIZONTAL,variable = var_b)
    panel_3.add(textbox4)
    
    
    
    def bla():
        pass
    button1 = Button(panel_3,text="Why I have this size?", height = 0, width = 0, command= bla())
    panel_3.add(button1)
    button1.pack(fill=X, expand=1) # Only fits in X (horizontal direction), expands according to the panel
    
    
    
    
    tkinter.mainloop()
    

    Note that if you want to resize the scale widgets you can using the width and height parameters but this only works for geometry managers such as place(), in this case you can use the geometry manager pack() to maintain the relationship and expansion according to the panel, however you cannot change its width since being a geometry manager that controls and organizes the widgets you can only expand and change it the height of the scale using in this case width to change the height of the scale widget.

    For more information see more information about the geometry manager pack()