In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to achieve a dialog class in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Build a dialog box to modify the file name.
# build top-level window as dialog rename_dlg = Toplevel (takefocus=True) # specify window title rename_dlg.title ('Rename') # disable window resizing rename_dlg.resizable (width=False, height=False) # build Frame objects to accommodate Label and Entry objects # use Frame to adjust the Label/Entry area and the button area below 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) # build Frame objects to accommodate OK and Cancel buttons btn_frame = Frame (rename_dlg) btn_frame.grid (row=1, column=0, sticky='e') # Dialog controls when building button controls are passed through labmda expressions Path and file name information # the modified file name is obtained through fn_var.get when the [OK] button is pressed. Ok_btn = Button (btn_frame, text='OK', command= (lambda wicked renametrical dlg) pamphpathrecy squarfn: rename_ok (wrecedprect smemf.get ()) ok_btn.grid (row=0, column=0) # cancel button directly destroys window object cancel_btn=Button (btn_frame, text='Cancel', command=rename_dlg.destroy) cancel_btn.grid (row=0, column=1) # restricts rename_dlg to receive mouse and keyboard events This is the key to the implementation of modal dialog boxes. Rename_dlg.grab_set () # centers the dialog box relative to the root window center_window (rename_dlg, root) # launch dialog main loop rename_dlg.mainloop () # destroy dialog window rename_dlg.destroy ()
If you look at the code, you can see that line 7 through line 27 is the actual required functionality of FileBrowser, and the rest of the code is the processing needed to build any dialog box. The following uses a class to separate this part of the function:
Class Dialog (Toplevel): def _ init__ (self, ref, title): Toplevel.__init__ (self, takefocus=True) self.ref = ref # specify window title self.title (title) # prohibit window resizing self.resizable (width=False, height=False) self.create_widgets () # restrict rename_dlg to receive mouse and keyboard events This is the key to the implementation of modal dialog boxes. Self.grab_set () # centers the dialog box relative to the root window center_window (self, ref) # launches the dialog box main loop self.mainloop ()
Def create_widgets (self): pass
When you need to build your own dialog box, you just need to inherit Dialog and implement the create_widgets method:
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') # Update file list file_list.select_node (None)
Def create_widgets (self): # build Frame objects to accommodate Label and Entry objects # use Frame to adjust the Label/Entry area and the button area below 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) # build Frame objects to accommodate OK and Cancel buttons btn_frame = Frame (self) btn_frame.grid (row=1, column=0, sticky='e') # pass dialog controls when building button controls through labmda expressions Path and file name information # the modified file name is obtained through fn_var.get when the [OK] button is pressed. Ok_btn = Button (btn_frame, text='OK', command=self.__rename) ok_btn.grid (row=0, column=0) # cancel button directly destroys the window object cancel_btn = Button (btn_frame, text='Cancel', command=self.destroy) cancel_btn.grid (row=0, column=1)
As you can see, there are only FileBrowser-related parts left in the RenameDlg class. Another benefit of encapsulating this part of the functionality into classes is that the code that uses the dialog box becomes easier:
Def rename_current (self): path, selections = self.selected_files () if path: for fn in selections: dlg = RenameDialog (self, path, fn) self.select_node (None)
The above is how to achieve a dialog box class in the Python shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 230
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.