Python Tkinter 标准会话

来源:互联网 发布:知乎 电力电子就业方向 编辑:程序博客网 时间:2024/05/16 14:05

Standard Dialogs

Before we look at what to put in that application work area, let’s take a look at another important part of GUI programming: displaying dialogs and message boxes.

在思考往程序的工作区放置什么之前,让我们先看一下GUI编程中另外一个很重要的部分:展示会话和消息框

Starting with Tk 4.2, the Tk library provides a set of standard dialogs that can be used to display message boxes, and to select files and colors. In addition, Tkinter provides some simple dialogs allowing you to ask the user for integers, floating point values, and strings. Where possible, these standard dialogs use platform-specific mechanisms, to get the right look and feel.

从Tk4.2开始,Tk库就提供了一系列的标准会话用于展示消息框、选择文件和颜色。此外,Tkinter提供了一些简单的会话从用户去请求整数值、浮点值和字符串。这些标准会话使用平台特殊机制,从而尽可能的保障合适的外观和感觉。

Message Boxes

The tkMessageBox module provides an interface to the message dialogs.

tkMessageBox模块提供一个消息会话的接口。

The easiest way to use this module is to use one of the convenience functions: showinfoshowwarningshowerroraskquestion,askokcancelaskyesno, or askretrycancel. They all have the same syntax:

使用这个模块的最简单的方式就是去使用那些方便的函数:showinfo, showwarnning, showerror, askquestion, askokcancel, askyesno, 或者askretrycancel. 它们拥有一样的语法:

tkMessageBox.function(title, message [, options]).

The title argument is shown in the window title, and the message in the dialog body. You can use newline characters (“\n”) in the message to make it occupy multiple lines. The options can be used to modify the look; they are explained later in this section.

参数title显示在窗口标题栏,参数message被显示在会话主体。在message中使用换行符("\n")可以使用多行。options参数可以用来修改外观,稍后的章节中会做相关的介绍。

The first group of standard dialogs is used to present information. You provide the title and the message, the function displays these using an appropriate icon, and returns when the user has pressed OK. The return value should be ignored.

第一类标准会话框用于展示信息。这个函数将会使用一个合适的图标去展示title和message,当用户按下OK以后返回,返回值则被忽略。

Here’s an example:

这里有一个例子。

    try:        fp = open(filename)    except:        tkMessageBox.showwarning(            "Open file",            "Cannot open this file\n(%s)" % filename        )        return
The showinfo dialog
The showwarning dialog
The showerror dialog

The second group is used to ask questions. The askquestion function returns the strings “yes” or “no” (you can use options to modify the number and type of buttons shown), while the others return a true value of the user gave a positive answer (okyes, and retry, respectively).

第二类被用于询问问题。askquestion函数返回字符串'yes'或者'no'(可以使用options参数去修改按钮的数目和类型),而其他的函数会在用户给出一个积极答案(ok,yes和retry)的时候返回一个真值

 
    if tkMessageBox.askyesno("Print", "Print this report?"):        report.print()
The askquestion dialog
The askokcancel dialog
The askyesno dialog
The askretrycancel dialog

[Screenshots made on a Swedish version of Windows 95. Hope you don’t mind…]

Message Box Options

If the standard message boxes are not appropriate, you can pick the closest alternative (askquestion, in most cases), and use options to change it to exactly suit your needs. You can use the following options (note that message and title are usually given as arguments, not as options).

如果标准消息框是不合适的,可以选择最恰当的,并使用options参数去修改它从而满足的需求。下面的options可供选择(注意message和title通常用作参数,而非options)

default (constant)

Which button to make default: ABORTRETRYIGNOREOKCANCEL,YES, or NO (the constants are defined in the tkMessageBox module).

icon (constant)

Which icon to display: ERRORINFOQUESTION, or WARNING

message (string)

The message to display (the second argument to the convenience functions). May contain newlines.

parent (widget)

Which window to place the message box on top of. When the message box is closed, the focus is returned to the parent window.

title (string)

Message box title (the first argument to the convenience functions).

type (constant)

Message box type; that is, which buttons to display:ABORTRETRYIGNOREOKOKCANCELRETRYCANCELYESNO, or YESNOCANCEL.

0 0
原创粉丝点击