Skip to Content
《Python 编程》,第 4 版
book

《Python 编程》,第 4 版

by Mark Lutz
May 2025
Intermediate to advanced
1632 pages
29h 8m
Chinese
O'Reilly Media, Inc.
Content preview from 《Python 编程》,第 4 版

第 9 章tkinter 之旅,第二部分

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

"今天的菜单垃圾邮件、垃圾邮件和垃圾邮件"

本章是 tkinter 库两部分中的第二部分。它承接第 8 章的内容,介绍了 tkinter 库中一些更高级的部件和工具。本章介绍的主题包括

  • Menu,Menubutton, 和OptionMenu 小部件

  • Scrollbar 小工具:用于滚动文本、列表和画布

  • Listbox 部件:多选列表

  • Text 小工具:通用文本显示和编辑工具

  • Canvas 小工具:通用图形绘制工具

  • grid 基于表格的几何图形管理器

  • 基于时间的工具:after,update,wait, 和线程

  • 基本 tkinter 动画技术

  • 剪贴板、擦除小部件和 Windows 等

学完本章后,你就会了解 tkinter 库的全部内容,并掌握制作大型、可移植的用户界面所需的全部信息。你也将准备好应对第 10章和第 11 章中介绍的更大型的图形用户界面技术和更完整的示例。 现在,让我们继续小部件秀。

菜单是你习惯在 Windows 窗口(或整个显示屏,如果你习惯在 Macintosh 上这样看的话)顶部看到的下拉列表。将鼠标光标移到顶部的菜单栏,点击一个名称(如文件),在你点击的名称下面就会弹出一个可选项列表(如打开、保存)。菜单内的选项可能会触发操作,就像点击按钮一样;它们还可能打开其他 "级联 "子菜单,列出更多选项、弹出对话窗口等。在 tkinter 中,有两种菜单可以添加到脚本中:顶级窗口菜单和基于框架的菜单。前者更适合整个 Windows,但后者也可以作为嵌套组件使用。

顶层窗口菜单

在最近发布的所有 Python 版本中(使用 Tk 8.0 及更高版本),您可以将水平菜单栏与顶层窗口对象(如TkToplevel )关联起来。在 Windows 和 Unix (X Windows) 上,该菜单栏沿窗口顶部显示;在某些 Macintosh 机器上,当窗口被选中时,该菜单会取代屏幕顶部显示的菜单。换句话说,无论脚本运行在何种底层平台上,窗口菜单的显示方式都与预期的一样。

该方案基于建立Menu widget 对象树。只需将一个顶层Menu 与窗口关联,添加其他下拉式Menu 对象作为顶层Menu 的级联,并为每个下拉式对象添加条目。通过使用父窗口部件参数和Menu窗口部件的 add_cascade 方法,Menu与下一级交叉链接。工作原理如下

  1. 创建最顶部的Menu作为 Windows widget 的子窗口,并将窗口的menu 属性配置为新的Menu

  2. 对于每个下拉对象,新建一个Menu 作为最顶部Menu 的子对象,并使用add_cascade 将该子对象作为最顶部Menu的级联添加。

  3. 使用add_commandcommand 选项注册选择回调处理程序,为步骤 2 中的每个下拉菜单Menu 添加菜单选择。

  4. 创建一个新的Menu 作为级联扩展的Menu 的子菜单,并使用add_cascade 链接父菜单和子菜单,从而添加一个级联子菜单。

最终结果是一棵由Menu 小部件组成的树,并带有相关的command 回调处理程序。不过,代码可能比文字更简单。例 9-1中的主菜单有两个下拉菜单:文件和编辑;编辑下拉菜单又有一个嵌套的子菜单。

例 9-1.PP4E\Gui\Tour\menu_win.py
# Tk8.0 style top-level window menus from tkinter import * # get ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

实用 Python 数据整理与数据质量

实用 Python 数据整理与数据质量

Susan E. McGregor

Publisher Resources

ISBN: 9798341659612