Mad Lib程序

单选框  复选框  按钮  标签  文本框的应用

  1 #coding=utf-8
  2 __author__ = 'minmin'
  3 from Tkinter import *
  4 
  5 class Application(Frame):
  6     def __init__(self,master):
  7         Frame.__init__(self,master)
  8         self.grid()
  9         self.create_widgets()
 10 
 11     def create_widgets(self):
 12         #创建说明标签
 13         Label(self,text="Enter information for a new story").grid(row = 0,column = 0,columnspan = 2,sticky =W)
 14 
 15         #创建用于任命的标签和文本框
 16         Label(self,text="Person:").grid(row = 1,column = 0,sticky = W)
 17         self.person_ent =Entry(self)
 18         self.person_ent.grid(row = 1,column = 1,sticky = W)
 19 
 20         #创建用于复数名词的标签和文本框
 21         Label(self,text="Plural Noun:").grid(row = 2,column = 0,sticky = W)
 22         self.noun_ent =Entry(self)
 23         self.noun_ent.grid(row = 2,column = 1,sticky = W)
 24 
 25         #创建用于动词的标签和文本框
 26         Label(self,text="Verb:").grid(row = 3,column = 0,sticky = W)
 27         self.verb_ent =Entry(self)
 28         self.verb_ent.grid(row =3,column = 1,sticky = W)
 29 
 30         #创建用于形容词的复选框
 31         Label(self,text="Adjective(s):").grid(row = 4,column = 0,sticky = W)
 32 
 33         #创建itchy复选框
 34         self.is_itchy = BooleanVar()
 35         Checkbutton(self,text="itchy",variable=self.is_itchy).grid(row=4,column=1,sticky=W)
 36         #创建joyous复选框
 37         self.is_joyous = BooleanVar()
 38         Checkbutton(self,text="joyous",variable=self.is_joyous).grid(row=4,column=2,sticky=W)
 39         #创建electric复选框
 40         self.is_electric = BooleanVar()
 41         Checkbutton(self,text="electric",variable=self.is_electric).grid(row=4,column=3,sticky=W)
 42 
 43         #创建用于身体部位单选框的标签
 44         Label(self,text="Body Part:").grid(row=5,column=0,sticky=W)
 45 
 46         self.body_part = StringVar()
 47         self.body_part.set(None)
 48 
 49         #创建身体部位单选框
 50         body_parts =["bellybtton","big toe","medulla oblongata"]
 51         column =1
 52         for part in body_parts:
 53             Radiobutton(self,text=part,variable=self.body_part,value=part).grid(row=5,column = column,sticky = W)
 54             column +=1
 55 
 56         #提交按钮
 57         Button(self,text="Click for story",command = self.tell_story).grid(row=6,column = 0,sticky = W)
 58 
 59         self.result_text = Text(self,width = 75,height =10,wrap = WORD)
 60         self.result_text.grid(row=7,column=0,columnspan =4)
 61     def tell_story(self):
 62         #从GUI获取相应的值
 63         person = self.person_ent.get()
 64         noun = self.noun_ent.get()
 65         verb = self.verb_ent.get()
 66         adj =""
 67         if self.is_itchy.get():
 68             adj +="itchy,"
 69         if self.is_joyous.get():
 70             adj +="joyous,"
 71         if self.is_electric.get():
 72             adj +="electrci,"
 73         body_part = self.body_part.get()
 74 
 75         #创建故事
 76         story = "The famous explorer "
 77         story +=person
 78         story +=" had nearly given up a life-long quest to find The Lost City of "
 79         story +=noun.title()
 80         story +=" when one day ,the "
 81         story += noun
 82         story += " found "
 83         story +=person +". "
 84         story +="A strong, "
 85         story += adj
 86         story +="peculiar feeling overwhelmed the explorer. "
 87         story +="After all this time,the quest was finallly over.A tear came to "
 88         story += person +"'s "
 89         story += body_part + "."
 90         story +="And then ,the "
 91         story += noun
 92         story +=" promptly devoured "
 93         story += person +"."
 94         story +="The mpral of the story ? Becareful what you "
 95         story += verb
 96         story += " for."
 97 
 98         #显示故事
 99         self.result_text.delete(0.0,END)
100         self.result_text.insert(0.0,story)
101 
102 
103 #程序主体
104 root = Tk()
105 root.title("Longevity")
106 root.geometry("700x400")
107 app = Application(root)
108 root.mainloop()

posted @ 2015-12-16 19:50  指间ゝ繁华初逝的格调  阅读(373)  评论(0编辑  收藏  举报