Python中怎么实现一个对话框类

发布时间:2021-07-05 16:06:41 作者:Leah
来源:亿速云 阅读:383

这期内容当中小编将会给大家带来有关Python中怎么实现一个对话框类,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

构建一个修改文件名对话框。

# 构建顶层窗口作为对话框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()

如果观察代码可以知道,从第7行到第27行为止是FileBrowser的实际需要的功能,其他的代码则是构建任何对话框都需要的处理。以下用用一个类将这部分功能独立出来:

class Dialog(Toplevel):    def __init__(self, ref, title):        Toplevel.__init__(self, takefocus=True)        self.ref = ref        # 指定窗口标题        self.title(title)        # 禁止窗口尺寸调整        self.resizable(width=False, height=False)        self.create_widgets()        # 限定rename_dlg接收鼠标和键盘事件,这是实现模态对话框的关键。        self.grab_set()        # 使对话框相对于root窗口居中        center_window(self, ref)        # 启动对话框主循环        self.mainloop()
    def create_widgets(self):        pass

当需要构建自己的对话框时只要继承Dialog可并实现create_widgets方法即可:

class RenameDialog(Dialog):    def __init__(self, file_list, path, fn):        self.path = path        self.fn = fn        self.fn_var = StringVar()        self.fn_var.set(self.fn)        Dialog.__init__(self, file_list.winfo_toplevel(), 'Rename File')        # 更新文件列表        file_list.select_node(None)
    def create_widgets(self):        # 构建Frame对象以容纳Label和Entry对象        # 使用Frame可以分别调整Label/Entry区域和下面的按钮区域        fn_frame = Frame(self)        fn_frame.grid(row=0, column=0)        Label(fn_frame, text='File Name:').grid(row=0, column=0)        fn_entry = Entry(fn_frame, textvariable=self.fn_var)        fn_entry.grid(row=0, column=1)        # 构建Frame对象以容纳OK和Cancel按钮        btn_frame = Frame(self)        btn_frame.grid(row=1, column=0, sticky='e')        # 通过labmda表达式传递构建按钮控件时的对话框控件,路径和文件名信息        # 修改后的文件名要在按下【OK】按钮是通过fn_var.get获取。        ok_btn = Button(btn_frame, text='OK', command=self.__rename)        ok_btn.grid(row=0, column=0)        # 取消按钮直接销毁窗口对象        cancel_btn = Button(btn_frame, text='Cancel', command=self.destroy)        cancel_btn.grid(row=0, column=1)

可以看到,RenameDlg类中只剩下FileBrowser相关的部分了。将这部分功能封装成类的另一个好处是使用对话框的代码变得简单了:

def rename_current(self):    path, selections = self.selected_files()    if path:        for fn in selections:            dlg = RenameDialog(self, path, fn)        self.select_node(None)

上述就是小编为大家分享的Python中怎么实现一个对话框类了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Python中怎么利用GUI实现一个输入对话框
  2. 使用Python怎么实现一个类装饰器

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

python

上一篇:phpstudy mysql数据库启动不了的解决方法

下一篇:Python中Sizegrip控件如何使用

相关阅读

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

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