Monday, 11 April 2016

Python GUI Programming - Part 1

PYTHON PROGRAMMING - GUI

Why is Python so popular?
Python is a high level language, so it is the easiest to learn. Moreover, out of all Hll, it's the best because:
1) It's cross platform
2) It can handle complex problems
3) It can perform heavy tasks, which "C" use to handle lately

Requirements 
1) A computer
2) Python installed
3) A good text-editor or Python IDE(like: PyCharm)  

Can Python make GUI applications?
Yes. It makes cross platform GUI applications, ie: you can create an application and it will run on Linux, Windows, Debian, Mac, etc...

#Lesson 1: Basic Commands - Print
 Open Python IDLE for this basic lesson. Type:

print("This is a text");

hit enter
It will print "This is a text" in the IDLE...

#Lesson 2: Basic Commands - Math
 Open Python IDLE and type:

4+4 

hit enter
It will print "8" as an answer. You can do the same for every math function.. Just like you do in your copies in schools :P

GUI PROGRAMMING
GUI programming is easy with python but not as easy as with other IDE's. So, you need to download "PyCharm" - The easiest and best python IDE

So let's make a basic Message Box on Button click:

1) Open PyCharm
2) Under "Untitled"(or any name given to path) right click
3)  New > File (not Python file!)
4) Now name it anything.py (.py is necessary)

Now type the following:

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
def hello():tkMessageBox.showinfo("Say Hello", "Hello World")


B1 = Tkinter.Button(top, text = "Say Hello", command = hello)
B1.pack()

top.mainloop()

Dont worry about error lines. They're just there ;)

Now right click and "Run" the file. Save it before that..
Now you can see a small GUI with button and when you click it, you get a message. So easy! 

I'll explain more things in "Part 2" 

Bye! 

No comments :

Post a Comment

Super Blog Directory