Python realizes message box and uses tkinter to realize window centering

Function: button + message box to realize random selection.

import random
import tkinter
import tkinter.messagebox

#消息弹窗
def button():
    list = ['Python','C','Java']
    num = random.randint(0,2)
    tkinter.messagebox.showinfo('提示','选择的是'+list[num])

def frame_center(window, Width, Hight):
    '''
        设置窗口居中和宽高
        :param window:主窗体
        :param Width:窗口宽度
        :param Hight:窗口高度
        :return:无
    '''
    # 获取屏幕宽度和高度
    sw = window.winfo_screenwidth()
    sh = window.winfo_screenheight()

    # 计算中心坐标
    cen_x = (sw - Width) / 2
    cen_y = (sh - Hight) / 2

    # 设置窗口大小并居中
    window.geometry('%dx%d+%d+%d' % (Width, Hight, cen_x, cen_y))

if __name__=='__main__':
    win=tkinter.Tk()
    win.title('Task')#标题
    frame_center(win,600,450)#设置窗体居中和窗体大小
    win.resizable(False, False)#固定窗体
    tkinter.Button(win, text='Please click!',command=button).pack()
    win.mainloop()

The renderings are as follows:
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45251017/article/details/123877496