Python: Basic GUI Application Dedicated For Beginners
Submitted by razormist on Friday, April 7, 2017 - 17:53.
In this tutorial we will create a simple application using Python basic GUI. Python supports packages and modules, which encourage a developer to program in a modularity and reusable way. Python lets you work more quickly than other programming languages, because of its syntax readability and simplified modules. So let's now do the coding.
Getting started:
First you will have to download & install the Python IDLE's, here the link for the Integrated Development And Learning Environment for python https://www.python.org/downloads/.
After you installed the Pythons IDLE's, run the IDLE and then click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
Importing Module
To create the application you need to import first a module called tkinter. Tkinter is the standard toolkit in python for creating GUI(Graphical User Interface). Write the code that I provided below into the text editor.
Creating the Window Form
This code will generate a blank window form that you will in generating the interface. To do that just simple copy the code below and paste into the text editor new line.
Note: In Python the code must be align to its proper indention to prevent errors in the future.
You'll notice in the code above that there is no instruction terminator at the end of the line. It is because Python made the programming more simplified so the developer don't have to worry about a miss semi colon all throughout.
The Main Function
This code is the main function to make the applications worked. To do that copy the code below and paste it inside the class application.
Initializing the App
This code will initialize the class called Application, and display Application.
There you have it we create a simple GUI application using Python. I hope that this tutorial help you to projects. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
- from tkinter import *
- class Application(Frame):
- def __init__(self, master):
- Frame.__init__(self, master)
- self.grid()
- self.main()
- def main(self):
- Label(self, text = "Choose your favorite programming languages", fg = "red").grid(row = 0, column = 0, sticky = W)
- Label(self, text = "Please check the choice you want:").grid(row = 2, column = 0, sticky = W)
- #C++
- self.cplusplus = BooleanVar()
- Checkbutton(self, text = "C++", variable = self.cplusplus, command = self.insert_text).grid(row = 3, column = 0, sticky = W)
- #C
- self.c = BooleanVar()
- Checkbutton(self, text = "C", variable = self.c, command = self.insert_text).grid(row = 4, column = 0, sticky = W)
- #Python
- self.python = BooleanVar()
- Checkbutton(self, text = "Python", variable = self.python, command = self.insert_text).grid(row = 5, column = 0, sticky = W)
- #C#
- self.csharp = BooleanVar()
- Checkbutton(self, text = "C#", variable = self.csharp, command = self.insert_text).grid(row = 6, column = 0, sticky = W)
- #Ruby
- self.ruby = BooleanVar()
- Checkbutton(self, text = "Ruby", variable = self.ruby, command = self.insert_text).grid(row = 7, column = 0, sticky = W)
- #Perl
- self.perl = BooleanVar()
- Checkbutton(self, text = "Perl", variable = self.perl, command = self.insert_text).grid(row = 8, column = 0, sticky = W)
- self.result = Text(self, width = 40, height = 5, wrap = WORD)
- self.result.grid(row = 9, column = 0, columnspan = 3)
- #VB
- self.vb = BooleanVar()
- Checkbutton(self, text = "VB", variable = self.vb, command = self.insert_text).grid(row = 9, column = 0, sticky = W)
- self.result = Text(self, width = 40, height = 5, wrap = WORD)
- self.result.grid(row = 9, column = 0, columnspan = 3)
- def insert_text(self):
- likes = ""
- if self.cplusplus.get():
- likes += "C++ "
- if self.c.get():
- likes += "C "
- if self.python.get():
- likes += "Python "
- if self.csharp.get():
- likes += "C# "
- if self.ruby.get():
- likes += "Ruby "
- if self.perl.get():
- likes += "Perl "
- if self.vb.get():
- likes += "VB "
- root = Tk()
- root.title("Programming Languages")
- app = Application(root)
- if __name__ == '__main__':
- app.mainloop()
Add new comment
- 150 views