Use the Python-Tkinter library for GUI programming and complete two functions:
(1) Ask the user to enter a name and age and print it out
(2) Require the user to enter a number and then calculate 1 to the number of the and
Code section:
# Import all the contents of Tkinter in all packages fromTkinter Import *Import Tkinter.messagebox asmessagebox# derives from frame a application class, which is the parent container for all widgetsclassApplication (Frame): def __init__ (Self,master=None): frame.__init__ (Self,master) Self.pack () self.createwidgets () def createwidgets (self): # module 1, set user input one character, alert bullet box output Hello+the character Self.hellolabel= Label (self, text='Module 1: Please enter your name and age, the program will be printed out'The self.helloLabel.pack () # Pack () method adds widgets to the parent container and implements the layout. Self.nameinput=Entry (self) self.nameInput.pack () Self.ageinput=Entry (self) self.ageInput.pack () Self.alertbutton= Button (self,text='Submit', command=Self.hello) Self.alertButton.pack () # module 2, set the user to enter a number, the Alert bullet box calculates the multiple of the number Self.hellolabel= Label (self, text='Module 2: Entering any number will calculate 1 to the number and the'The self.helloLabel.pack () # Pack () method adds widgets to the parent container and implements the layout. Self.numberinput=Entry (self) self.numberInput.pack () Self.alertbutton= Button (self,text='Submit', command=self.sum) Self.alertButton.pack () # Exit button settings Self.quitbutton= Button (self, text='Exit', command=self.quit) self.quitButton.pack () def hello (self): name= Self.nameinput.Get() or' World'# Get user-entered content age= Self.ageinput.Get() or -Messagebox.showinfo ('Personal Information','Name:%s\n Age:%s Years'%(name,age) # Call user input and print out def sum (self): number=int(Self.numberinput.Get()) # Gets the content entered by the user sum=0 forIinchRange (number): I+=1sum+=I messagebox.showinfo ('sum result','1 to%s and%s'% (number,sum) # calls the user input (number *2) and print it out # Instantiate app=application () # set window title: App.master.title ('Hello World') # main message loop App.mainloop ()
Execution effect:
Python Gadget-Alert bullet box output name age, sum