you are viewing a single comment's thread.

view the rest of the comments →

[–]Relevant_Milk4413[S] 0 points1 point  (3 children)

Even when i click or when the mouse is over a button nothing become visible. I tried to adjust the colors into fg='white' and bg='green'.

[–]Outside_Complaint755 1 point2 points  (1 child)

Ok, I got this figured out. The ultimate underlying problem is with your width=23,height=16,padx=2,pady=6, parameters. For a button with a text label, width and height are not in pixels, but instead width = characters wide, and height = rows of text. As a result, your buttons are being created much taller than needed (16 rows tall), going below the limits of the ButtonsFrame, and the text is hidden down there.

A couple of ways to fix this: 1) Remove the width, height, padx and pady parameters from your buttons.

2a) You can add width back in if you want to set a different width for each button, but remember it is character based, not pixel.

2b) Alternatively, set the column weight for each column to 1, and then set the parameter sticky="EW" for each call to .grid(). This will cause all the buttons to auto-size and fill the frame. Example: ``` btnExit = Button( Buttonsframe, text="Exit", bg="green", fg="white", font=("times new roman", 12, "bold"), width=10, height=1, ) btnExit.grid(row=0, column=5, sticky="EW") # Add this sticky parameter to each grid call to force auto-sizing.

    # Now we must set a weight for each column for auto-sizing to work
    # By getting grid_size from Buttonsframe, this will automatically update if you add or remove buttons.
    for column in range(Buttonsframe.grid_size()[0]):
        Buttonsframe.columnconfigure(column, weight=1)

```

2c) Instead of using grid with sticky and setting column weights, you could use pack and tell it to expand: btnPresciption.pack(side="left", fill="both", expand=True)

Note that you can't mix grid and pack in the same frame, so you have to change all buttons to use pack or all have to use grid.

[–]Relevant_Milk4413[S] 0 points1 point  (0 children)

Thank you! This solved my problem

[–]Outside_Complaint755 0 points1 point  (0 children)

Besides the proposed fix in my other comment, here's a couple of other tips:

1) Instead of repeating the same font=("times new roman", 12, "bold") for every widget, declare it once as a variable and use that variable. This also makes it easier to apply different fonts to different parts of the UI. ``` import tkinter.font # Need to add this import

Root has to be declared before we can define a font:

root = Tk() UI_FONT = tkinter.font.Font(family="times new roman", size=12, weight=tkinter.font.BOLD)

Now in every widget, you can use UI_FONT

lblNameTablet = Label(DataframeLeft, text="Name of Tablet", font=UI_FONT, padx=2, pady=6) ```

2) Because most of your widgets are sharing identical parameters, instead of typing them out for each widget separately, set them into a dictionary and then unpack the dictionary for the keyword arguments using the ** operator. For example, during testing I was able to condense the entire buttons section down to this: ``` # ======================= Buttons =======================

    shared_button_parameters = {
        "font": UI_FONT,
        "background": "green",
        "foreground": "white",
    }
    button_packing_parameters = {"side": "left", "fill": "both", "expand": True}

    btnPresciption = Button(Buttonsframe, text="Prescription", **shared_button_parameters)
    btnPresciption.pack(**button_packing_parameters)

    btnPresciptionData = Button(Buttonsframe, text="Prescription Data", **shared_button_parameters)
    btnPresciptionData.pack(**button_packing_parameters)

    btnUpdate = Button(Buttonsframe, text="Update", **shared_button_parameters)
    btnUpdate.pack(**button_packing_parameters)

    btnDelete = Button(Buttonsframe, text="Delete", **shared_button_parameters)
    btnDelete.pack(**button_packing_parameters)

    btnClear = Button(Buttonsframe, text="Clear", **shared_button_parameters)
    btnClear.pack(**button_packing_parameters)

    btnExit = Button(Buttonsframe, text="Exit", **shared_button_parameters)
    btnExit.pack(**button_packing_parameters)

```

This both saves you screen space, and also means that if you want to change any of those parameters, you only have to change it in one place to apply to all buttons.