Sunday, December 3, 2017

Tkinter

The following code is all that is needed to make a simple window

# this makes a basic window

import tkinter

top=tkinter.Tk()
top.mainloop()

Here is the form it makes.

Let's do a simple hello world. We will make the window a class that inherits from Frame. The __init__ function calls the parent __init__function. We add a Label, an Entry (text box) and a Button. When clicked the button produces a message box that says "Hello," plus the name entered into the Entry control. Here is the complete code with comments:

# this makes a basic window

from tkinter import *
from tkinter import messagebox


class Window(Frame):
    #we are inheriting from frame
    #and calling its init function
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master=master
        self.init_window()
    
    #set up the window
    def init_window(self):
        #set title of the window
        self.master.title("Hello")
        #self.pack(fill=BOTH, expand=1)
        # a label to prompt the user
        self.prompt=Label(self.master, text="Enter your name")
        #using absolute placement
        self.prompt.place(x=10,y=25)
        #creating a text box
        self.nameEntry=Entry(self.master)
        self.nameEntry.place(x=10,y=55)
        #adding a button
        self.submit=Button(self.master, text="submit", command=self.hello)
        self.submit.place(x=10,y=100)
        #self.result = Label(self.master,text="")
        #self.result.place(x=10, y=360)
        #self.result.pack()
        
    def hello(self):
        greeting = "Hello, " + self.nameEntry.get()
        messagebox.showinfo("hello", greeting)
        

     

        


root=Tk()
root.geometry=("500x400")

app=Window(root)
root.mainloop()



     

Here is the form with a name entered.

Here is the Messagebox

No comments:

Post a Comment