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 implement pop-up menus and message dialogs with Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I would like to share with you how to achieve Python pop-up menus and message dialog box related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Pop-up menus and message dialogs are also essential parts of the completed program. This article continues to take FileBrowser as an example to illustrate.

First get the function of the selected file information, which will be called in subsequent processing.

Def selected_files (): try: dir_node = tree_view.focus () if dir_node: path = node_path (dir_node) return path, map (lambda x:list_view.item (x, 'text'), list_view.selection () except Exception as e: showerror (' Error', str (e)) return None

The processing is mainly divided into two parts, one is to obtain the currently selected directory path through the current node of the tree_view, and the other is to obtain the selected file names (possibly multiple) in the list_view. The map method of the iterator is used in the code to convert the selected project iid directly to a list of file names.

The process of opening a file is simple, as long as you open the selected file in turn. The function of startfile is to open files using the default application of the operating system.

Def open_current (): path, selections = selected_files () if path: for fn in selections: os.startfile (os.path.join (path, fn))

The process of deleting the selected file first calls an askokcancel dialog box to confirm the user's true intention. Delete operation is performed after the user confirms it. Exception handling is used to handle deletion failures, such as when a Word file is opened in a video. When an exception occurs, the software displays an error message through a showerror dialog box.

The contents of the right-click processing function are as follows:

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) menu = Menu (list_view, tearoff=False) menu.add_command (label='Open', command=open_current) menu.add_command (label='Delete' Command=delete_current) menu.post (event.x_root, event.y_root)

The only parameter of the right mouse button handler is responsible for passing the mouse message, where event.x and event.y are the coordinates in the corresponding logical window, and evnet.x_root and event.y_root are the screen coordinates. The process of building a context menu is similar to a normal menu, except that you finally need to call the post method to start the menu. At this point, the coordinates passed to the post method need to use screen coordinates.

These are all the contents of the article "how to implement pop-up menus and message dialogs in Python". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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: 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