Python: Basic GUI Application Dedicated For Beginners

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.
  1. from tkinter import *
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.
  1. class Application(Frame):
  2. def __init__(self, master):
  3. Frame.__init__(self, master)
  4. self.grid()
  5. self.main()
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.
  1. def main(self):
  2. Label(self, text = "Choose your favorite programming languages", fg = "red").grid(row = 0, column = 0, sticky = W)
  3.  
  4. Label(self, text = "Please check the choice you want:").grid(row = 2, column = 0, sticky = W)
  5. #C++
  6. self.cplusplus = BooleanVar()
  7. Checkbutton(self, text = "C++", variable = self.cplusplus, command = self.insert_text).grid(row = 3, column = 0, sticky = W)
  8. #C
  9. self.c = BooleanVar()
  10. Checkbutton(self, text = "C", variable = self.c, command = self.insert_text).grid(row = 4, column = 0, sticky = W)
  11.  
  12. #Python
  13. self.python = BooleanVar()
  14. Checkbutton(self, text = "Python", variable = self.python, command = self.insert_text).grid(row = 5, column = 0, sticky = W)
  15.  
  16. #C#
  17. self.csharp = BooleanVar()
  18. Checkbutton(self, text = "C#", variable = self.csharp, command = self.insert_text).grid(row = 6, column = 0, sticky = W)
  19.  
  20. #Ruby
  21. self.ruby = BooleanVar()
  22. Checkbutton(self, text = "Ruby", variable = self.ruby, command = self.insert_text).grid(row = 7, column = 0, sticky = W)
  23.  
  24. #Perl
  25. self.perl = BooleanVar()
  26. Checkbutton(self, text = "Perl", variable = self.perl, command = self.insert_text).grid(row = 8, column = 0, sticky = W)
  27.  
  28. self.result = Text(self, width = 40, height = 5, wrap = WORD)
  29. self.result.grid(row = 9, column = 0, columnspan = 3)
  30.  
  31. #VB
  32. self.vb = BooleanVar()
  33. Checkbutton(self, text = "VB", variable = self.vb, command = self.insert_text).grid(row = 9, column = 0, sticky = W)
  34.  
  35. self.result = Text(self, width = 40, height = 5, wrap = WORD)
  36. self.result.grid(row = 9, column = 0, columnspan = 3)
  37.  
  38. def insert_text(self):
  39. likes = ""
  40.  
  41. if self.cplusplus.get():
  42. likes += "C++ "
  43. if self.c.get():
  44. likes += "C "
  45. if self.python.get():
  46. likes += "Python "
  47. if self.csharp.get():
  48. likes += "C# "
  49. if self.ruby.get():
  50. likes += "Ruby "
  51. if self.perl.get():
  52. likes += "Perl "
  53. if self.vb.get():
  54. likes += "VB "
Initializing the App This code will initialize the class called Application, and display Application.
  1. root = Tk()
  2. root.title("Programming Languages")
  3. app = Application(root)
  4.  
  5. if __name__ == '__main__':
  6. app.mainloop()
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!!

Tags

Add new comment