General Discussions

Basic GUI in Python


Indian Bloggers

This post helps you in building a basic GUI for python using tkinter. Python 3.4 is used for coding.

# Import the tkinter
from tkinter import *

root = Tk()
var = StringVar()

# Set the Layout as desired and the title for the window
root.geometry("1000x600")
root.title("Title Here")

# Display an image
# The image gets displayed at the centre of the window
image = PhotoImage(file="give-image-name-with-path.gif")
labelp = Label(image=image)
labelp.pack()
labelp.place(relx=0.5, rely=0.2, anchor=CENTER)

# Display a text prompt say asking a name
key_display = Label(root, text="Enter the name")
key_display.pack()
key_display.place(relx=0.2, rely=0.4, anchor=CENTER)

# Set a input box to collect a user input
# The input can be extratced from the variable 'var'
user_input = Entry(root, bd=7,textvariable=var)
user_input.pack()
user_input.place(relx=0.5, rely=0.4, anchor=CENTER)

# Get a button at the centre
B = Button(root, text ="-- Button Name --", command=function_name_you_want_to_exe)
B.pack()
B.place(relx=0.5, rely=0.5, anchor=CENTER, height=50, width=150)

root.mainloop()
Advertisements

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s