⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 213.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Designing Applications</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="206.html" class="navtitle">15. Tkinter</a> &gt; <span class="nonavtitle">Designing Applications</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="212.html" title="Tkinter Widgets"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=213" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="213.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="214.html" title="PMW桺ython Mega Widgets"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A50%3A08+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162149150217133007126133082</font><a href="read3.asp?bookname=0672319942&snode=213&now=5%2F31%2F2002+4%3A50%3A08+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>







Designing Applications</h3>
				<p>Up to this point, we've seen how to handle the properties and methods of Tkinter's widgets. Now, we will learn the basic steps to write real-world applications.</p>

				<P>Tkinter is really powerful, and if you are not satisfied with the widgets that it offers, you can create your own set of widgets. A very interesting and customized widget that you should consider checking before learning how to create your own, is the TreeWidget, which is part of the latest idle distribution. This widget uses a Tk Canvas widget and some images to nicely simulate the TreeView Windows control.</P>

				<P>The simplest windowing application that you can create consists of just one <A name="idx1073749330"></A>
					<A NAme="idx1073749331"></a>
					<a NAME="idx1073749332"></a>
					<a name="idx1073749333"></a>window, which is called the root window. The root window is created using the <tt class="monofont">Tk()</tt> call.</p>

				<pre>
					
from Tkinter import *
root = Tk()
root.mainloop()

				</Pre>

				<P>If your application needs more than just one single window, you can use the Toplevel widget to create additional windows for you. This widget has a behavior very similar to the window generated by <tt cLass="monofont">Tk().</tT> This widget also dispenses the use of geometry management functions because the window manager displays this widget, immediately after you call it.</p>

				<prE>
					
from Tkinter import *
def mywindow():
    top = Toplevel(root)

root = Tk()
b1 = Button(root, text="Create new window", command=mywindow)
b1.pack()
root.mainloop()

				</PRE>

				<p>After adding a lot of windows to your application, maybe now you are wondering whether it would be OK to add a <a naME="idx1073749334"></A>
					<A name="idx1073749335"></A>
					<A NAme="idx1073749336"></a>menu to your program. The following code does that for you.</p>

				<PRE>
					
from Tkinter import *
import sys
def newwindow():
    top = Toplevel(root)
def aboutwindow():
    who = Toplevel(root)
    Label(who, text="This is the about window").pack()

root = Tk()
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newwindow)


filemenu.add_separator()
filemenu.add_command(label="Exit", command=sys.exit)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=aboutwindow)

root.mainloop()

				</Pre>

				<p>What's next? What about adding a <a name="idx1073749337"></a>
					<a name="idx1073749338"></a>
					<a name="idx1073749339"></A>toolbar to our little application? The simplest way to implement a toolbar is by taking a Frame widget and storing all the required buttons on it.</p>

				<pRe>
					
from Tkinter import *
import sys
def newwindow():
    top = Toplevel(root)
def aboutwindow():
    who = Toplevel(root)
    Label(who, text="This is the about window").pack()

root = Tk()
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newwindow)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=sys.exit)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=aboutwindow)

toolbar = Frame(root)
newimage = PhotoImage(file="new.gif")
b1 = Button(toolbar, image=newimage, width=16, command=newwindow)
b1.pack(side=LEFT, padx=1, pady=1)
helpimage = PhotoImage(file="help.gif")
b2 = Button(toolbar, image=helpimage, width=16, command=aboutwindow)
b2.pack(side=LEFT, padx=1, pady=1)
toolbar.pack(side=TOP, fill=X)
root.mainloop()

				</prE>

				<p>As we want our toolbar to be on the highest area of our screen, we have to pack it on the top side of the Frame widget. The <tt cLass="monofont">fill</TT> option being set to <TT clasS="monofont">X</TT> in the toolbar widget enables the toolbar to extend itself, covering the entire extension of the parent frame size.<A name="idx1073749340"></A>
					<A NAme="idx1073749341"></a>
					<a NAME="idx1073749342"></a>
					<a name="idx1073749343"></a>
					<a name="idx1073749344"></a>
					<a name="idx1073749345"></a>
					<a NamE="idx1073749346"></a>
					<a nAme="idx1073749347"></a>
				</p>

				<P>Note the usage of the PhotoImage class. This class is used to load the GIF files from disk and store them into variables. Then, these variables are passed to the Button options that handle images.</p>

				<p>Let's move forward now. The next step is to <a NAME="idx1073749348"></a>
					<a naME="idx1073749349"></A>
					<A name="idx1073749350"></A>create a status bar for our small application. We want this bar to be on the bottom side of the window.</P>

				<PRe>
					
from Tkinter import *
import sys
def newwindow():
    top = Toplevel(root)
    statusbar.config(text="This is a testing application.")

def aboutwindow():
    who = Toplevel(root)
    Label(who, text="This is the about window").pack()
    statusbar.config(text="Hi There!")

root = Tk()
menu = Menu(root)
root.config(menu=menu)

filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newwindow)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=sys.exit)

helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=aboutwindow)

toolbar = Frame(root)
newimage = PhotoImage(file="new.gif")
b1 = Button(toolbar, image=newimage, width=16, command=newwindow)
b1.pack(side=LEFT, padx=1, pady=1)
helpimage = PhotoImage(file="help.gif")
b2 = Button(toolbar, image=helpimage, width=16, command=aboutwindow)


b2.pack(side=LEFT, padx=1, pady=1)
toolbar.pack(side=TOP, fill=X)

statusbar = Label(root, text="This is a testing application.", bd=1,
 relief=SUNKEN, anchor=W)
statusbar.pack(side=BOTTOM, fill=X)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -