from Tkinter import *
from tkMessageBox import *
class EntryDemo( Frame):
""" Demonstrate Entrys and Envent Building"""
def __init__( self ):
Frame.__init__(self)
self.pack(expand=YES,fill = BOTH)
self.master.title("Testing Entry Components")
self.master.geometry("600x300") #width X length
#Entry Demo
self.frame1 = Frame(self)
self.frame1.pack( pady = 5 ) #垂直间距
self.text1 = Entry(self.frame1, name="text1") #放置在frame1上
self.text1.bind("<Return>",self.showContents )
self.text1.pack(side=LEFT, padx=5)
self.text2 = Entry(self.frame1, name="text2")
self.text2.insert(INSERT, "Enter text here")
self.text2.bind("<Return>",self.showContents )
self.text2.pack(side=LEFT,padx=5)
self.frame2=Frame(self)
self.frame2.pack(pady=5)
self.text3=Entry(self.frame2, name="text3")
self.text3.insert(INSERT,"Uneditable text field")
self.text3.config(state=DISABLED)
self.text3.bind("<Return>",self.showContents )
self.text3.pack(side=LEFT,padx=5)
self.text4=Entry(self.frame2, name="text4", show="*")
self.text4.insert(INSERT,"Hidden text")
self.text4.bind("<Return>",self.showContents )
self.text4.pack(side=LEFT, padx=5)
#button Demo
self.frame3= Frame(self)
self.frame3.pack(pady = 5)
self.plainButton=Button(self.frame3, text="Planin Button",\
command= self.pressedPlain)
self.plainButton.bind("<Enter>", self.rolloverEnter) #鼠标事件:进入
self.plainButton.bind("<Leave>", self.rolloverLeave) #鼠标事件:离开
self.plainButton.pack(side=LEFT,padx=5, pady=5)
#Check button Demo
self.frame4= Frame(self)
self.frame4.pack(pady = 5)
self.text5 = Entry(self.frame4, width=40, font = "Arial 10")
self.text5.insert(INSERT, "Watch the font Style change")
self.text5.pack(padx=5, pady=5)
self.frame5= Frame(self)
self.frame5.pack(pady = 5)
self.boldOn=BooleanVar()
self.checkBold=Checkbutton(self.frame5,text = "Bold",variable=self.boldOn, command=self.changeFont)
self.checkBold.pack(side=LEFT,padx=5,pady=5)
self.italicOn=BooleanVar()
self.checkItalic=Checkbutton(self.frame5,text="Italic",variable=self.italicOn,command=self.changeFont)
self.checkItalic.pack(side=LEFT, padx=5, pady=5)
#radio button Demo
self.frame6= Frame(self)
self.frame6.pack(pady = 5)
self.text6 = Entry(self.frame6, width=40, font = "Arial 10")
self.text6.insert(INSERT, "Watch the font Style change--radio button")
self.text6.pack(padx=5, pady=5)
self.frame7= Frame(self)
self.frame7.pack(pady = 5)
fontSelections = ["Plain", "Bold", "Italic","Bold/Italic"]
self.chosenFont=StringVar()
#initial selection
self.chosenFont.set(fontSelections[0])
for style in fontSelections:
aButton = Radiobutton(self.frame7, text=style,variable=self.chosenFont, value=style,command = self.changeFontRadio)
aButton.pack(side = LEFT, padx=5, pady=5)
def pressedPlain(self):
showinfo("Message","You pressed Plain Button")
def rolloverEnter(self,event):
event.widget.config(relief=GROOVE)
def rolloverLeave(self,event):
event.widget.config(relief=RAISED)
def changeFont(self):
desiredFont="Arial 10"
if self.boldOn.get():
desiredFont += " bold"
if self.italicOn.get():
desiredFont += " italic"
self.text5.config(font=desiredFont)
def changeFontRadio(self):
desiredFont="Arial 10"
if self.chosenFont.get() == "Bold":
desiredFont += " bold"
elif self.chosenFont.get()=="Italic":
desiredFont += " italic"
elif self.chosenFont.get()=="Bold/Italic":
desiredFont += " bold italic"
self.text6.config(font = desiredFont)
def showContents(self, event):
theName=event.widget.winfo_name()
theContents=event.widget.get()
showinfo("Message", theName +":"+ theContents)
def main():
EntryDemo().mainloop()
if __name__=="__main__":
main()
|