viewing paste Unknown #27692 | Text

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import tkinter
""" sets up a gui that accepts the user's name and greets them when a button is pressed
"""
 
class MyFrame(tkinter.Frame):
    def __init__(self):
        """ Initializes the Frame by putting the widgets on it """
        tkinter.Frame.__init__(self)
        self.pack()
        self.createWidgets()
 
    def createWidgets(self):
        """ Instantiates all of the widgets and places them onto the frame """
        self.nameEntry = tkinter.Entry()
        self.nameEntry.insert(0, "your name here")
        self.nameEntry.pack({"side": "left"})
        
        self.quitButton = tkinter.Button(self)
        self.quitButton["text"] = "Quit"
        self.quitButton["command"] =  self.quit
        self.quitButton.pack({"side": "left"})
 
        self.hiButton = tkinter.Button(self)
        self.hiButton["text"] = "Hello",
        self.hiButton["command"] = self.sayHi
        self.hiButton.pack({"side": "left"})
 
        self.greeting = tkinter.Label(self)
        self.greeting.pack({"side": "left"})
        
    def sayHi(self):
        """ greets the user by taking the text from the nameEntry field and putting it into the greeting field """
        self.greeting["text"] = "Hello " + self.nameEntry.get()
 
if __name__ == "__main__":
    root = tkinter.Tk()
    app = MyFrame()
    app.mainloop()
    root.destroy()
Viewed 902 times, submitted by Guest.