The python tkinter runtime grid function executes normally directly with commands that are not executed but are buttons

dodo266q 注册会员
2023-02-25 12:17

Let's take these frames out to see if they can be displayed normally. How do they look?
Also check width =(root.winfo_width() -21) / 3,height =(root.winfo_height() -33) / 3.

dlyss10000 注册会员
2023-02-25 12:17

windowSet function I have but I haven't typed it out, this problem should have nothing to do with this function

dazui9813 注册会员
2023-02-25 12:17

You can use the create_window() method to associate a position in the Canvas with a child control. In your code, you can add each child control to the Canvas with the following statement:

main.create_window((i%3*(root.winfo_width()-21)/3, i//3*(root.winfo_height()-33)/3), window=globals()[f"f_{i+1}"], anchor="nw")

Here, i%3*(root.winfo_width()-21)/3 and i//3*(root.winfo_height()-33)/3 Figure out where the child controls are in the Canvas. The anchor parameter specifies the anchor point of the child control, that is, the upper-left corner of the child control is aligned with the specified point on the Canvas.

xiaowenzi12 注册会员
2023-02-25 12:17

This answer quotes ChatGPT

Based on the code you provided and the results you ran, you can see that the button's command calls the windowSet function through a lambda expression, passing main to the function as an argument. Inside the windowSet function, it may modify the controls in f_main, but your code does not update the Canvas scrollregion property, which causes the Canvas to not know the size of the area to scroll.

To fix this, add code in the windowSet function to update the Canvas's scrollregion property after f_main is updated, as follows:

def windowSet(root,savedSettings,f_main):
    # do something to modify f_main
    f_main.update_idletasks()  # update f_main to get its new size
    root.update_idletasks()  # update root to get its new size

Here, the update_idletasks() method is called to ensure that all window controls have been updated, and canvas_main.bbox("all") returns a tuple representing the bounding box of all the child controls in Canvas, And use it as the value of the scrollregion property.

This way, when the windowSet function finishes executing, the Canvas will be able to display all the controls in f_main correctly, and the scrollbar will work as expected.

About the Author

Question Info

Publish Time
2023-02-25 12:17
Update Time
2023-02-25 12:17