Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the dialog box in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Python how to customize the dialog box, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

First, add the modify file name menu item to the pop-up menu:

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)

Line 11 first determines the number of files selected, and if it is 1, add the [Rename] menu item. When the user selects [Rename], the system calls the rename_current function, which is implemented as follows:

Def rename_current (): path, selections = selected_files () if path: for fn in selections: # build the top-level window as a dialog rename_dlg = Toplevel (takefocus=True) # specify the 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') # pass dialog box 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= (lambda walled renametrical dlg) pamphpathrecy slicfn: rename_ok (wrecoversmeme fnbearvar.get ()) ok_btn.grid (row=0, column=0) # cancel button directly destroys the window object cancel_btn=Button (btn_frame, text='Cancel') Command=rename_dlg.destroy) cancel_btn.grid (row=0, column=1) # qualifies rename_dlg to receive mouse and keyboard events This is the key to the implementation of modal dialog boxes. Rename_dlg.grab_set () # Center the dialog box relative to the root window center_window (rename_dlg, root) # launch dialog box main loop rename_dlg.mainloop () # destroy dialog window rename_dlg.destroy () # Update file list select_node (None)

The author has written a detailed comment line, please understand it yourself with the code. The center_window function can be used directly in other situations, and its implementation is as follows:

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)

If the reference window object ref is specified, the window is adjusted to the center of the ref, otherwise to the center of the screen.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 0

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report