文件操作 - index.html
返回文件管理
返回主菜单
删除本文件
文件: /usr/share/doc/python2-easygui-0.96/tutorial/index.html
编辑文件内容
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 4.0//EN" "html.dtd"> <HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"> <META NAME="resource-type" CONTENT="document"> <TITLE>EasyGui Tutorial</TITLE> <STYLE REL="stylesheet" TYPE="text/css"> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BEGIN easygui_webpage.css style for EasyGui web pages. Uses sans-serif fonts for screen readability. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> html { font-family: FreeSans /* for Linux browsers */ , "Lucida Sans Unicode" , "Verdana" , "Tahoma" , "Helvetica" , "arial" , "sans-serif" , "proportional" ; color: rgb(56,56,56); background-color: rgb(230,230,230); } h1 {text-align: center} h2, h3, h4, h5 {text-align: left} .subtitle {text-align:center;} h1 {text-align: center} note {font-size: small} td.heading{text-align:center} td {text-align: left; vertical-align:top} th {text-align:center; vertical-align:top} hr { background-color: #D2B48C; /* #D2B48C = tan */ border-color: #D2B48C; width: 100%; height: 0.5em; border-style: solid; } pre{ background-color:#FFFFFF; margin:10px; padding: 10px } <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ END easygui_webpage.css ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> </STYLE> </HEAD> <BODY> <H1>EasyGui Tutorial</H1> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MENUBAR begin ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <TABLE WIDTH="100%" CELLPADDING="5" CELLSPACING="0"> <TR><TD BGCOLOR="lightgrey" VALIGN="TOP" ALIGN="left"> <A HREF="http://easygui.sourceforge.net">EasyGui Home</A> <A HREF="../index.html">Version Home</A> <A HREF="../pydoc/easygui_pydoc.html">Pydoc Docs</A> <A HREF="../epydoc/index.html">Epydoc Docs</A> <A HREF="../tutorial/index.html">Tutorial</A> <A HREF="../cookbook/index.html">Cookbook</A> <A HREF="../faq/index.html">FAQs</A> <A HREF="http://www.ferg.org/contact_info/index.html">Contact</A> </TD> </TR> </TABLE> <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MENUBAR end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ --> <DIV CLASS="table_of_contents"> <!-- ~~~~~~~~~~~~~~~~~~~~ BEGIN TABLE OF CONTENTS ~~~~~~~~~~~~~~~~~~~~ --> <!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Table of contents was automatically generated by program: pyhat.py. Generation date/time: 2009-06-23 17:32:07 For more information: visit www.ferg.org/pyhat ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--> <A ID="contents_item_0"></A> <H2>Table of Contents</H2> <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0"> <TR> <TD WIDTH="4%"></TD><TD WIDTH="4%"></TD><TD WIDTH="4%"></TD><TD WIDTH="4%"></TD><TD WIDTH="4%"></TD><TD WIDTH="80%"></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">1</A> <A HREF="#contents_item_1" ID="table_of_contents_1">A warning about using EasyGui with IDLE</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">2</A> <A HREF="#contents_item_2" ID="table_of_contents_2">Introduction</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">3</A> <A HREF="#contents_item_3" ID="table_of_contents_3">EasyGui's demonstration routine</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">4</A> <A HREF="#contents_item_4" ID="table_of_contents_4">Importing EasyGui</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">5</A> <A HREF="#contents_item_5" ID="table_of_contents_5">Using EasyGui</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">6</A> <A HREF="#contents_item_6" ID="table_of_contents_6">Default arguments for EasyGui functions</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">7</A> <A HREF="#contents_item_7" ID="table_of_contents_7">Using keyword arguments when calling EasyGui functions</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8</A> <A HREF="#contents_item_8" ID="table_of_contents_8">Using buttonboxes</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8.1</A> <A HREF="#contents_item_8.1" ID="table_of_contents_8.1">msgbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8.2</A> <A HREF="#contents_item_8.2" ID="table_of_contents_8.2">ccbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8.3</A> <A HREF="#contents_item_8.3" ID="table_of_contents_8.3">ynbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8.4</A> <A HREF="#contents_item_8.4" ID="table_of_contents_8.4">buttonbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8.5</A> <A HREF="#contents_item_8.5" ID="table_of_contents_8.5">indexbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">8.6</A> <A HREF="#contents_item_8.6" ID="table_of_contents_8.6">boolbox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">9</A> <A HREF="#contents_item_9" ID="table_of_contents_9">How to show an image in a buttonbox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">10</A> <A HREF="#contents_item_10" ID="table_of_contents_10">Letting the user select from a list of choices</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">10.1</A> <A HREF="#contents_item_10.1" ID="table_of_contents_10.1">choicebox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">10.2</A> <A HREF="#contents_item_10.2" ID="table_of_contents_10.2">multchoicebox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">11</A> <A HREF="#contents_item_11" ID="table_of_contents_11">Letting the user enter information</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">11.1</A> <A HREF="#contents_item_11.1" ID="table_of_contents_11.1">enterbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">11.2</A> <A HREF="#contents_item_11.2" ID="table_of_contents_11.2">integerbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">11.3</A> <A HREF="#contents_item_11.3" ID="table_of_contents_11.3">multenterbox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">12</A> <A HREF="#contents_item_12" ID="table_of_contents_12">Letting the user enter password information</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">12.1</A> <A HREF="#contents_item_12.1" ID="table_of_contents_12.1">passwordbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">12.2</A> <A HREF="#contents_item_12.2" ID="table_of_contents_12.2">multpasswordbox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">13</A> <A HREF="#contents_item_13" ID="table_of_contents_13">Displaying text</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">13.1</A> <A HREF="#contents_item_13.1" ID="table_of_contents_13.1">textbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">13.2</A> <A HREF="#contents_item_13.2" ID="table_of_contents_13.2">codebox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">14</A> <A HREF="#contents_item_14" ID="table_of_contents_14">Working with files</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">14.1</A> <A HREF="#contents_item_14.1" ID="table_of_contents_14.1">diropenbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">14.2</A> <A HREF="#contents_item_14.2" ID="table_of_contents_14.2">fileopenbox</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">14.3</A> <A HREF="#contents_item_14.3" ID="table_of_contents_14.3">filesavebox</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">15</A> <A HREF="#contents_item_15" ID="table_of_contents_15">Remembering User Settings</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">15.1</A> <A HREF="#contents_item_15.1" ID="table_of_contents_15.1">EgStore</A></DIV></TD> </TR> <TR><TD COLSPAN="1"> </TD><TD COLSPAN="5"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">16</A> <A HREF="#contents_item_16" ID="table_of_contents_16">Trapping Exceptions</A></DIV></TD> </TR> <TR><TD COLSPAN="2"> </TD><TD COLSPAN="4"> <DIV STYLE="text-align:left; text-indent:-3em; margin-left:3em; font-family: sans-serif"><A HREF="#contents_item_0">16.1</A> <A HREF="#contents_item_16.1" ID="table_of_contents_16.1">exceptionbox</A></DIV></TD> </TR> </TABLE> <!-- ~~~~~~~~~~~~~~~~~~~~ _END_ TABLE OF CONTENTS ~~~~~~~~~~~~~~~~~~~~ --> </DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_1" ID="contents_item_1"> 1</A> </SPAN>A warning about using EasyGui with IDLE</H2> <P> You may encounter problems using IDLE to run programs that use easygui. Try it and find out. easygui is a collection of Tkinter routines that run their own event loops. IDLE is also a Tkinter application, with its own event loop. The two may conflict, with the unpredictable results. If you find that you have problems, try running your program outside of IDLE. </P> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_2" ID="contents_item_2"> 2</A> </SPAN>Introduction</H2> <P> In easygui, all GUI interactions are invoked by simple function calls. </P> <P> Here is a simple demo program using easygui. The screens that it produces are shown on the <A HREF="index.html">easygui home page</A>.</P> <PRE> from easygui import * import sys while 1: msgbox("Hello, world!") msg ="What is your favorite flavor?" title = "Ice Cream Survey" choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"] choice = choicebox(msg, title, choices) # note that we convert choice to string, in case # the user cancelled the choice, and we got None. msgbox("You chose: " + str(choice), "Survey Result") msg = "Do you want to continue?" title = "Please Confirm" if ccbox(msg, title): # show a Continue/Cancel dialog pass # user chose Continue else: sys.exit(0) # user chose Cancel</PRE> <H2 ID="demo_routine"><SPAN CLASS="contents_item"><A HREF="#table_of_contents_3" ID="contents_item_3"> 3</A> </SPAN>EasyGui's demonstration routine</H2> <P> To run EasyGui's demonstration routine, invoke EasyGui from the command line this way: </P> <PRE> python easygui.py </PRE> <P>or from an IDE (such as IDLE, PythonWin, Wing, etc.) this way:</P> <PRE> from easygui import * egdemo()</PRE> <P> This will allow you to try out the various EasyGui functions, and will print the results of your choices to the console. </P> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_4" ID="contents_item_4"> 4</A> </SPAN>Importing EasyGui</H2> <P> In order to use EasyGui, you must import it. The simplest import statment is: </P> <PRE> import easygui</PRE> <P> If you use this form, then to access the EasyGui functions, you must prefix them with the name "easygui", this way: </P> <PRE> easygui.msgbox(...)</PRE> <P> One alternative is to import EasyGui this way: </P> <PRE> from easygui import *</PRE> <P> This makes it easier to invoke the EasyGui functions; you won't have to prefix the function names with "easygui". You can just code something like this: </P> <PRE> msgbox(...)</PRE> <P> A third alternative is to use something like the following import statement: </P> <PRE> import easygui as g</PRE> <P> This allows you to keep the EasyGui namespace separate with a minimal amount of typing. You can access easgui functions like this: </P> <PRE> g.msgbox(...)</PRE> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_5" ID="contents_item_5"> 5</A> </SPAN>Using EasyGui</H2> <P> Once your module has imported EasyGui, GUI operations are a simple a matter of invoking EasyGui functions with a few parameters. For example, using EasyGui, the famous "Hello, world!" program looks like this: </P> <PRE> from easygui import * msgbox("Hello, world!")</PRE> <P> To see a demo of what EasyGui output looks like, invoke EasyGui from the command line,this way: </P> <PRE> python easygui.py</PRE> <P> To see examples of code that invokes the EasyGui functions, look at the demonstration code at the end of easygui.py. </P> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_6" ID="contents_item_6"> 6</A> </SPAN>Default arguments for EasyGui functions</H2> <P>For all of the boxes, the first two arguments are for message and title, in that order. In some cases, this might not be the most user-friendly arrangment (for example, the dialogs for getting directory and filenames ignore the message argument), but I felt that keeping this consistent across all widgets was a consideration that is more important.</P> <P>Most arguments to EasyGui functions have defaults. Almost all of the boxes display a message and a title. The title defaults to the empty string, and the message usually has a simple default.</P> <P> This makes it is possible to specify as few arguments as you need in order to get the result that you want. For instance, the title argument to msgbox is optional, so you can call msgbox specifying only a message, this way: </P> <PRE> msgbox("Danger, Will Robinson!")</PRE> <P> or specifying a message and a title, this way: </P> <PRE> msgbox("Danger, Will Robinson!", "Warning!")</PRE> <P> On the various types of buttonbox, the default message is "Shall I continue?", so you can (if you wish) invoke them without arguments at all. Here we invoke ccbox (the close/cancel box, which returns a boolean value) without any arguments at all. </P> <PRE> if ccbox(): pass # user chose to continue else: return # user chose to cancel</PRE> <DIV ID="keyword_arguments"> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_7" ID="contents_item_7"> 7</A> </SPAN>Using keyword arguments when calling EasyGui functions</H2> <P>It is possible to use keyword arguments when calling EasyGui functions. <I>(Prior to version 0.80, only the use of positional arguments was documented. In version 0.80, parameter names were modified to be more consistent, and the use of keyword arguments was documented.) </I></P> <P>Suppose for instance that you wanted to use a buttonbox, but (for whatever reason) did not want to specify the <I>title</I> (second) positional argument. You could still specify the <I>choices</I> argument (the third argument) using a keyword, this way:</P> <PRE>choices = ["Yes","No","Only on Friday"] reply = choicebox("Do you like to eat fish?", choices=choices)</PRE></DIV> <DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8" ID="contents_item_8"> 8</A> </SPAN>Using buttonboxes</H2> <P> There are a number of functions built on top of buttonbox() for common needs. </P> <BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8.1" ID="contents_item_8.1"> 8.1</A> </SPAN>msgbox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-msgbox">msgbox</A> displays a message and offers an OK button. You can send whatever message you want, along with whatever title you want. You can even over-ride the default text of "OK" on the button if you wish. Here is the signature of the msgbox function: </P> <PRE>def msgbox(msg="(Your message goes here)", title="", ok_button="OK"): ....</PRE><BR>The clearest way to over-ride the button text is to do it with a keyword argument, like this: <PRE>msgbox("Backup complete!", ok_button="Good job!")</PRE> <P>Here are a couple of examples</P> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> <PRE> msgbox("Hello, world!") </PRE></TD> <TD WIDTH="60%"><IMG SRC="screenshot_msgbox.png" WIDTH="360" HEIGHT="112"></TD> </TR> <TR> <TD WIDTH="60%"> <PRE> msg = "Do you want to continue?" title = "Please Confirm" if ccbox(msg, title): # show a Continue/Cancel dialog pass # user chose Continue else: # user chose Cancel sys.exit(0) </PRE></TD> <TD><IMG SRC="screenshot_ccbox.png" WIDTH="360" HEIGHT="112"></TD> </TR> </TABLE></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8.2" ID="contents_item_8.2"> 8.2</A> </SPAN>ccbox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-ccbox">ccbox</A> offers a choice of Continue and Cancel, and returns either 1 (for continue) or 0 (for cancel).</P> <P> NOTE that this box returns integer values (1 and 0), not true boolean values (true and false), which became available in Python version 2.3. </P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8.3" ID="contents_item_8.3"> 8.3</A> </SPAN>ynbox</H3> <BLOCKQUOTE> <P> <A HREF="../pydoc/easygui.html#-ynbox">ynbox</A> offers a choice of Yes and No, and returns either 1 (for yes)or 0 (for no). </P> <P> NOTE that this box returns integer values (1 and 0), not true boolean values (true and false), which became available in Python version 2.3. </P></BLOCKQUOTE> </BLOCKQUOTE> </DIV> <DIV> <BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8.4" ID="contents_item_8.4"> 8.4</A> </SPAN>buttonbox</H3> <BLOCKQUOTE> <P> To specify your own set of buttons in a buttonbox, use the buttonbox() function. </P> <P> The buttonbox can be used to display a set of buttons of your choice. When the user clicks on a button, buttonbox() returns the text of the choice. If the user cancels or closes the buttonbox, the default choice (the first choice) is returned. </P> <P> <A HREF="../pydoc/easygui.html#-buttonbox">buttonbox</A> displays a message, a title, and a set of buttons. Returns the text of the button that the user selected.</P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8.5" ID="contents_item_8.5"> 8.5</A> </SPAN>indexbox</H3> <BLOCKQUOTE> <P> <A HREF="../pydoc/easygui.html#-indexbox">indexbox</A> displays a message, a title, and a set of buttons. Returns the index of the user's choice. For example, if you invoked index box with three choices (A, B, C), indexbox would return 0 if the user picked A, 1 if he picked B, and 2 if he picked C. </P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_8.6" ID="contents_item_8.6"> 8.6</A> </SPAN>boolbox</H3> <BLOCKQUOTE> <P> <A HREF="../pydoc/easygui.html#-boolbox">boolbox</A> (boolean box) displays a message, a title, and a set of buttons. Returns returns 1 if the first button is chosen. Otherwise returns 0. </P></BLOCKQUOTE> </BLOCKQUOTE> <BLOCKQUOTE> <BLOCKQUOTE> <P> Here is a simple example of a boolbox(): </P> <PRE> message = "What does she say?" title = "" if boolbox(message, title, ["She loves me", "She loves me not"]): sendher("Flowers") else: pass </PRE> </BLOCKQUOTE></BLOCKQUOTE></DIV> <DIV ALIGN="LEFT" ID="buttonbox_with_image"> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_9" ID="contents_item_9"> 9</A> </SPAN>How to show an image in a buttonbox</H2> <P> When you invoke the buttonbox function (or other functions that display a button box, such as <I>msgbox</I>, <I>indexbox</I>, <I>ynbox</I>, etc.), you can specify the keyword argument <I>image=xxx</I> where <I>xxx</I> is the filename of a .gif image. <SPAN STYLE="color:red;">Note that .gif is the only format currently supported.</SPAN></P> <P> If an <I>image</I> argument is specified, the image file will be displayed after the message.</P> <P>Here is some sample code from <A HREF="#demo_routine">EasyGui's demonstration routine</A>.</P> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%" ALIGN="left"> <PRE>image = "python_and_check_logo.gif" msg = "Do you like this picture?" choices = ["Yes","No","No opinion"] reply=buttonbox(msg,image=image,choices=choices) </PRE></TD> <TD WIDTH="60%"><IMG SRC="screenshot_buttonbox_with_image.png" WIDTH="408" HEIGHT="222"></TD> </TR> </TABLE></DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_10" ID="contents_item_10"> 10</A> </SPAN>Letting the user select from a list of choices</H2> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_10.1" ID="contents_item_10.1"> 10.1</A> </SPAN>choicebox</H3> <P> Buttonboxes are good for offering the user a small selection of short choices. But if there are many choices, or the text of the choices is long, then a better strategy is to present them as a list. </P> <P> <A HREF="../pydoc/easygui.html#-choicebox">choicebox</A> provides a way for a user to select from a list of choices. The choices are specified in a sequence (a tuple or a list). The choices will be given a case-insensitive sort before they are presented. </P> <P> The keyboard can be used to select an element of the list. </P> <P> Pressing "g" on the keyboard, for example, will jump the selection to the first element beginning with "g". Pressing "g" again, will jump the cursor to the next element beginning with "g". At the end of the elements beginning with "g", pressing "g" again will cause the selection to wrap around to the beginning of the list and jump to the first element beginning with "g". </P> <P> If there is no element beginning with "g", then the last element that occurs before the position where "g" would occur is selected. If there is no element before "g", then the first element in the list is selected. </P> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> <PRE> msg ="What is your favorite flavor?" title = "Ice Cream Survey" choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"] choice = choicebox(msg, title, choices) </PRE></TD> <TD><A HREF="screenshot_choicebox_icecream.png"> <IMG SRC="screenshot_choicebox_icecream.png" WIDTH="500" TITLE="choicebox under Win2k. Click to enlarge."><BR>choicebox under Win2k. Click to enlarge.</A></TD> </TR> </TABLE> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> Another example of a choicebox </TD> <TD><A HREF="screenshot_choicebox.png"> <IMG SRC="screenshot_choicebox.png" WIDTH="500" TITLE="choicebox under Win2k. Click to enlarge."> <BR>choicebox under Win2k. Click to enlarge.</A></TD> </TR> </TABLE> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_10.2" ID="contents_item_10.2"> 10.2</A> </SPAN>multchoicebox</H3> <P>The <A HREF="../pydoc/easygui.html#-multchoicebox">multchoicebox</A>() function provides a way for a user to select from a list of choices. The interface looks just like the choicebox, but the user may select zero, one, or multiple choices. </P> <P>The choices are specified in a sequence (a tuple or a list). The choices will be given a case-insensitive sort before they are presented.</P> </TD> <TD><A HREF="screenshot_multchoicebox.png"> <IMG SRC="screenshot_multchoicebox.png" WIDTH="500" TITLE="multchoicebox under Win2. Click to enlarge."> <BR>multchoicebox under Win2. Click to enlarge.</A><BR></TD> </TR> </TABLE> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_11" ID="contents_item_11"> 11</A> </SPAN>Letting the user enter information</H2> <DIV> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_11.1" ID="contents_item_11.1"> 11.1</A> </SPAN>enterbox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-enterbox">enterbox</A> is a simple way of getting a string from the user</P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_11.2" ID="contents_item_11.2"> 11.2</A> </SPAN>integerbox</H3> <BLOCKQUOTE> <P> <A HREF="../pydoc/easygui.html#-integerbox">integerbox</A> is a simple way of getting an integer from the user.</P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_11.3" ID="contents_item_11.3"> 11.3</A> </SPAN>multenterbox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-multenterbox">multenterbox</A> is a simple way of showing multiple enterboxes on a single screen. </P></BLOCKQUOTE> </TD><TD> <A HREF="screenshot_multenterbox_vista.png"> <IMG SRC="screenshot_multenterbox_vista.png" WIDTH="500" TITLE="multenterbox under Vista. Click to enlarge."> <BR>multenterbox under Vista. Click to enlarge.</A> </TD> </TR> </TABLE> <DIV> <P><B>In the multenterbox :</B></P> <P>If there are fewer values than names, the list of values is padded with empty strings until the number of values is the same as the number of names. </P> <P> If there are more values than names, the list of values is truncated so that there are as many values as names. </P> <P> Returns a list of the values of the fields, or None if the user cancels the operation. </P> <P> Here is some example code, that shows how values returned from multenterbox can be checked for validity before they are accepted. </P> <PRE> msg = "Enter your personal information" title = "Credit Card Application" fieldNames = ["Name","Street Address","City","State","ZipCode"] fieldValues = [] # we start with blanks for the values fieldValues = multenterbox(msg,title, fieldNames) # make sure that none of the fields was left blank while 1: if fieldValues == None: break errmsg = "" for i in range(len(fieldNames)): if fieldValues[i].strip() == "": errmsg = errmsg + ('"%s" is a required field.\n\n' % fieldNames[i]) if errmsg == "": break # no problems found fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues) print "Reply was:", fieldValues </PRE> <P> Here is some example code, that shows how values returned from multpasswordbox can be checked for validity before they are accepted. </P> <PRE> msg = "Enter logon information" title = "Demo of multpasswordbox" fieldNames = ["Server ID", "User ID", "Password"] fieldValues = [] # we start with blanks for the values fieldValues = multpasswordbox(msg,title, fieldNames) # make sure that none of the fields was left blank while 1: if fieldValues == None: break errmsg = "" for i in range(len(fieldNames)): if fieldValues[i].strip() == "": errmsg = errmsg + ('"%s" is a required field.\n\n' % fieldNames[i]) if errmsg == "": break # no problems found fieldValues = multpasswordbox(errmsg, title, fieldNames, fieldValues) print "Reply was:", fieldValues</PRE> </DIV> </DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_12" ID="contents_item_12"> 12</A> </SPAN>Letting the user enter password information</H2> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_12.1" ID="contents_item_12.1"> 12.1</A> </SPAN>passwordbox</H3> <P>A <A HREF="../pydoc/easygui.html#-passwordbox">passwordbox</A> box is like an enterbox, but used for entering passwords. The text is masked as it is typed in. </P> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_12.2" ID="contents_item_12.2"> 12.2</A> </SPAN>multpasswordbox</H3> <P><A HREF="../pydoc/easygui.html#-multpasswordbox">multpasswordbox</A> has the same interface as multenterbox, but when it is displayed, the last of the fields is assumed to be a password, and is masked with asterisks. </P></TD> <TD><IMG SRC="screenshot_passwordbox.png" WIDTH="500"></TD> </TR> </TABLE> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_13" ID="contents_item_13"> 13</A> </SPAN>Displaying text</H2> <DIV> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%"> <P>EasyGui provides functions for displaying text. </P> <BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_13.1" ID="contents_item_13.1"> 13.1</A> </SPAN>textbox</H3> <BLOCKQUOTE> <P>The <A HREF="../pydoc/easygui.html#-textbox">textbox</A>() function displays text in a proportional font. The text will word-wrap. </P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_13.2" ID="contents_item_13.2"> 13.2</A> </SPAN>codebox</H3> <BLOCKQUOTE> <P> The <A HREF="../pydoc/easygui.html#-codebox">codebox</A>() function displays text in a monospaced font and does not wrap.</P></BLOCKQUOTE> </BLOCKQUOTE> </TD><TD> <A HREF="screenshot_codebox_vista.png"> <IMG SRC="screenshot_codebox_vista.png" WIDTH="500" TITLE="codebox under Vista. Click to enlarge."> <BR>codebox under Vista. Click to enlarge.</A> </TD> </TR> </TABLE> <P> Note that you can pass codebox() and textbox() either a string or a list of strings. A list of strings will be converted to text before being displayed. This means that you can use these functions to display the contents of a file this way: </P> <PRE> filename = os.path.normcase("c:/autoexec.bat") f = open(filename, "r") text = f.readlines() f.close() codebox("Contents of file " + filename, "Show File Contents", text) </PRE> </DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_14" ID="contents_item_14"> 14</A> </SPAN>Working with files</H2> <DIV> <P> A common need is to ask the user for a filename or for a directory. EasyGui provides a few basic functions for allowing a user to navigate through the file system and choose a directory or a file. (These functions are wrappers around widgets and classes in lib-tk.) </P> <P>Note that in the current version of EasyGui, the startpos argument is not supported. </P> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%" VALIGN="top"> <BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_14.1" ID="contents_item_14.1"> 14.1</A> </SPAN>diropenbox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-diropenbox">diropenbox</A> returns the name of a directory</P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_14.2" ID="contents_item_14.2"> 14.2</A> </SPAN>fileopenbox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-fileopenbox">fileopenbox</A> returns the name of a file</P></BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_14.3" ID="contents_item_14.3"> 14.3</A> </SPAN>filesavebox</H3> <BLOCKQUOTE> <P><A HREF="../pydoc/easygui.html#-filesavebox">filesavebox</A> returns the name of a file</P></BLOCKQUOTE> </BLOCKQUOTE> </TD> <TD> <A HREF="screenshot_fileopenbox_vista.png"> <IMG SRC="screenshot_fileopenbox_vista.png" WIDTH="500" TITLE="fileopenbox under Vista. Click to enlarge."> <BR>fileopenbox under Vista. Click to enlarge.</A> </TD> </TR> </TABLE> </DIV> <DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_15" ID="contents_item_15"> 15</A> </SPAN>Remembering User Settings</H2> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_15.1" ID="contents_item_15.1"> 15.1</A> </SPAN>EgStore</H3> <P> A common need is to ask the user for some setting, and then to "persist it", or store it on disk, so that the next time the user uses your application, you can remember his previous setting. </P> <P>In order to make the process of storing and restoring user settings, EasyGui provides a class called <A HREF="../pydoc/easygui.html#-EgStore">EgStore</A>. In order to remember some settings, your application must define a class (let's call it <I>Settings</I>, although you can call it anything you want) that inherits from EgStore.</P> <P>Your application must also create an object of that class (let's call the object <I>settings</I>). </P> <P>The constructor (the __init__ method) of the Settings class must initialize all of the values that you wish to remember. </P> <P>Once you have done this, you can remember the settings simply by assigning values to instance variables in the settings object, and use the settings.store() method to persist the settings object to disk.</P> <P>Here is an example of code to create the <I>Settings</I> class:</P> <PRE> #----------------------------------------------------------------------- # define a class named Settings as a subclass of EgStore #----------------------------------------------------------------------- class Settings(EgStore): def __init__(self, filename): # filename is required #------------------------------------------------- # Specify default/initial values for variables that # this particular application wants to remember. #------------------------------------------------- self.userId = "" self.targetServer = "" #------------------------------------------------- # For subclasses of EgStore, these must be # the last two statements in __init__ #------------------------------------------------- self.filename = filename # this is required self.restore() # restore values from the storage file if possible </PRE> <P>Here is an example of code to create the <I>settings</I> object. Simply creating the <I>settings</I> object will restore its values from the settingsFile, if the settingsFile exists.</P> <PRE> #----------------------------------------------------------------------- # create "settings", a persistent Settings object # Note that the "filename" argument is required. # The directory for the persistent file must already exist. #----------------------------------------------------------------------- settingsFilename = os.path.join("C:", "myApp", "settings.txt") # Windows example settings = Settings(settingsFilename) </PRE> <P>And here is example code of using the <I>settings</I> object.</P> <PRE> # we initialize the "user" and "server" variables # In a real application, we'd probably have the user enter them via enterbox user = "obama_barak" server = "whitehouse1" # we save the variables as attributes of the "settings" object settings.userId = user settings.targetServer = server settings.store() # persist the settings # run code that gets a new value for userId # then persist the settings with the new value user = "biden_joe" settings.userId = user settings.store()</PRE></DIV> <DIV> <H2><SPAN CLASS="contents_item"><A HREF="#table_of_contents_16" ID="contents_item_16"> 16</A> </SPAN>Trapping Exceptions</H2> <TABLE BORDER="1" CELLPADDING="5" ALIGN="center" VALIGN="top" WIDTH="100%" CELLSPACING="0" RULES="rows" FRAME="VOID"> <TR> <TD WIDTH="60%" VALIGN="top"> <BLOCKQUOTE> <H3><SPAN CLASS="contents_item"><A HREF="#table_of_contents_16.1" ID="contents_item_16.1"> 16.1</A> </SPAN>exceptionbox</H3> <P> Sometimes exceptions are raised... even in EasyGui applications. Depending on how you run your application, the stack trace might be thrown away, or written to stdout while your application crashes.</P> <P>EasyGui provides a better way of handling exceptions via <A HREF="../pydoc/easygui.html#-exceptionbox">exceptionbox</A>. Exceptionbox displays the stack trace in a codebox and may allow you to continue processing.</P> <P>Exceptionbox is easy to use. Here is a code example.</P> <PRE>try: someFunction() # this may raise an exception except: exceptionbox()</PRE> </BLOCKQUOTE> </TD> <TD> <A HREF="screenshot_exceptionbox_vista.png"> <IMG SRC="screenshot_exceptionbox_vista.png" WIDTH="500" TITLE="exceptionbox under Vista. Click to enlarge."> <BR>exceptionbox under Vista. Click to enlarge.</A> </TD> </TR> </TABLE> </DIV> </BODY> </HTML>
修改文件时间
将文件时间修改为当前时间的前一年
删除文件