python中tkinter界面居中,为什么运行开始时会在初始位置闪烁一下再居中?代码如下,望高手指点一下!!

发布网友

我来回答

2个回答

热心网友

    以前写的,你看看是否有所帮助

def center_window(self,master,width_flag = 0.382,height_flag = 0.382):

        """

        窗口先隐藏到大小设置完成以后才恢复,主要原因是如果不这么做,会发生闪影现象。

        width_flag 和 height_flag 值在 (0,1) ,是定位目标左上角的坐标的权重值。

        都设置为 0.5 的话,则窗口居中。

        withdraw() 函数是隐藏窗口,deiconify() 函数是显示窗口。

        update() 函数是将前面原件摆放以后的窗口更新,以便获得摆放后窗口的自适配大小。

        """

        master.withdraw()

        master.update()

        current_window_width = master.winfo_width()

        current_window_height = master.winfo_height()

        screen_width = master.winfo_screenwidth()

        screen_height = master.winfo_screenheight()

        suitable_location_x = int((screen_width - current_window_width)*width_flag)

        suitable_location_y = int((screen_height - current_window_height)*height_flag)

        master.geometry('+{}+{}'.format(suitable_location_x,suitable_location_y))

        master.deiconify()

热心网友

编程语言初始化GUI时都会有一定的延迟。一般只是快慢问题。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com