# Movie Chooser 2
# Demonstrates radio buttons
from tkinter import *
class Application(Frame):
""" GUI Application for favorite movie type. """
def __init__(self, master):
""" Initialize Frame. """
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create widgets for movie type choices. """
# create description label
Label(self, text = "Enter information for a new story").grid(row = 0, column = 0, sticky = W)
Label(self, text = "Hero:").grid(row = 1, column = 0, sticky = W)
Label(self,text = "Enemy:").grid(row = 2, column = 0, sticky = W)
Label(self,text = "Verb:").grid(row = 3, column = 0, sticky = W)
Label(self,text = "Adjective(s):").grid(row = 4, column = 0, sticky = W)
Label(self,text = "Weapon:").grid(row = 5, column = 0,sticky = W)
# create text inpute
self.ent1 = Entry(self)
self.ent1.grid(row = 1, column = 1, sticky = W)
self.ent2 = Entry(self)
self.ent2.grid(row = 2, column = 1, sticky = W)
self.ent3 = Entry(self)
self.ent3.grid(row = 3, column = 1, sticky = W)
# create variable for single, favorite type of movie
self.Weapon = StringVar()
self.Weapon.set(None)
# create Checkbutton
self.handsome = BooleanVar()
self.respected = BooleanVar()
self.nimble = BooleanVar()
Checkbutton(self, text = "handsomen",variable = self.handsome).grid(row = 4, column = 1, sticky = W)
Checkbutton(self, text = "respected", variable = self.respected).grid(row = 4, column = 2, sticky = W)
Checkbutton(self, text = "nimble", variable = self.nimble).grid(row = 4, column = 3, sticky = W)
# create Comedy radio button
Radiobutton(self,text = "Sword",variable = self.Weapon,value = "sword.").grid(row = 5, column = 1, sticky = W)
Radiobutton(self,text = "Bow",variable = self.Weapon,value = "bow.").grid(row = 5, column = 2, sticky = W)
Radiobutton(self,text = "Saber",variable = self.Weapon,value = "saber.").grid(row = 5, column = 3, sticky = W)
# create Button
Button(self, text = "click for story",command = self.update_text).grid(row = 6, column = 0,sticky = W)
# create text field to display result
self.results_txt = Text(self, width = 80, height = 8,wrap = WORD)
self.results_txt.grid(row = 7, column = 0, columnspan = 4)
def update_text(self):
""" Update text area and display user's favorite movie type. """
adj = ""
if self.handsome.get():
adj += "handsome,"
if self.respected.get():
adj += "respected,"
if self.nimble.get():
adj += "nimble,"
story = "In the old days,There was a Here,his name was "+ self.ent1.get() +".He was "+ adj +" honest and brave.He lived in a small village which is quiet and pacific, One day,"+ self.ent1.get() +" went out to hunt,"+ self.ent2.get() +" attacked his village,robed wealth,burned crops and killed people.When Jack came back, he found that his hometown was destroied......He sweared revenge,then Jack took his "+ self.Weapon.get() +" and had a hard journey.Finally ,Jack found "+self.ent2.get()+".After a very difficult fight.Jack won and "+self.ent3.get()+" "+self.ent2.get()+".In the end,Jack came back and trid to reconstruct his home town."
#story += self.BodyPart.get()
self.results_txt.delete(0.0, END)
self.results_txt.insert(0.0, story)
# main
root = Tk()
root.title("Mad Lib")
root.geometry("600x300")
app = Application(root)
root.mainloop()
评论0