温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python中怎么自定义对话框

发布时间:2021-07-05 16:05:38 来源:亿速云 阅读:344 作者:Leah 栏目:大数据

Python中怎么自定义对话框,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

首先在弹出菜单中增加修改文件名菜单项:

def rbutton_down(event):    iid = list_view.identify_row(event.y)    if iid:        if iid not in list_view.selection():            list_view.selection_set(iid)            list_view.focus(iid)        path, selections = selected_files()        if path:            menu = Menu(list_view, tearoff=False)            menu.add_command(label='Open', command=open_current)            if len(list(selections))==1:                menu.add_command(label='Rename', command=rename_current)            menu.add_command(label='Delete', command=delete_current)            menu.post(event.x_root, event.y_root)

代码11行首先判断选中的文件个数,如果为1则增加【Rename】菜单项。当用户选择【Rename】时,系统会调用rename_current函数,其实现如下:

def rename_current():    path, selections = selected_files()    if path:        for fn in selections:            # 构建顶层窗口作为对话框            rename_dlg = Toplevel(takefocus=True)            # 指定窗口标题            rename_dlg.title('Rename')            # 禁止窗口尺寸调整            rename_dlg.resizable(width=False, height=False)            # 构建Frame对象以容纳Label和Entry对象            # 使用Frame可以分别调整Label/Entry区域和下面的按钮区域            fn_frame = Frame(rename_dlg)            fn_frame.grid(row=0,column=0)            Label(fn_frame, text='File Name:').grid(row=0, column=0)            fn_var = StringVar()            fn_var.set(fn)            fn_entry = Entry(fn_frame, textvariable=fn_var)            fn_entry.grid(row=0, column=1)            # 构建Frame对象以容纳OK和Cancel按钮            btn_frame = Frame(rename_dlg)            btn_frame.grid(row=1, column=0, sticky='e')            # 通过labmda表达式传递构建按钮控件时的对话框控件,路径和文件名信息            # 修改后的文件名要在按下【OK】按钮是通过fn_var.get获取。            ok_btn = Button(btn_frame, text='OK',                            command=(lambda w=rename_dlg,p=path,s=fn: rename_ok(w,p,s,fn_var.get())))            ok_btn.grid(row=0, column=0)            # 取消按钮直接销毁窗口对象            cancel_btn=Button(btn_frame, text='Cancel', command=rename_dlg.destroy)            cancel_btn.grid(row=0, column=1)            # 限定rename_dlg接收鼠标和键盘事件,这是实现模态对话框的关键。            rename_dlg.grab_set()            # 使对话框相对于root窗口居中            center_window(rename_dlg, root)            # 启动对话框主循环            rename_dlg.mainloop()            # 销毁对话框窗口            rename_dlg.destroy()            # 更新文件列表            select_node(None)

作者编写了详细的注释行,请结合代码自行理解。center_window函数的功能可以直接在其他场合使用,其实现如下:

def center_window(wnd, ref):    wnd.update()    width = wnd.winfo_width()    height = wnd.winfo_height()    if ref:        ref_width = ref.winfo_width()        ref_height = ref.winfo_height()        x = ref.winfo_x()        y = ref.winfo_y()        size = '%dx%d+%d+%d' % (width, height, x + (ref_width - width) / 2, y+(ref_height - height) / 2)    else:        s_width = wnd.winfo_screenwidth()        s_height = wnd.winfo_screenheight()        size = '%dx%d+%d+%d' % (width, height, (s_width - width) / 2, (s_height - height) / 2)    wnd.geometry(size)

如果指定了参照窗口对象ref则将窗口调整到ref的中心位置,否则调整到屏幕中心位置。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI