Python GUI Programming
Most of the programs we have done till now, are text based programming. But there are many
programs that need GUI (Graphical User Interface).
Python provides several different options for writing GUI based programs. These are listed below:
There are many other interfaces available for GUI. But these are the most commonly used ones. In this we will learn about the basic GUI programming using Tkinter.
Using Tkinter
It is the standard GUI toolkit for Python. It was written by Fredrik Lundh. For modern Tk binding, Tkinter is implemented as a Python wrapper for the Tcl Interpreter embedded within the interpreter of Python. Tk provide the following widgets:
Creating a GUI program using this Tkinter is simple. For this programmers need to follow the steps mentioned below:
A Sample Program Using Tkinter
In this program it is shown how Tkinter is used via Python to built windows along with some buttons and the events that is programmed using these buttons.
Example:
#!/usr/bin/python
import tkinter as tk
from tkinter import *
from tkinter import ttk
class karl( Frame ):
def __init__( self ):
tk.Frame.__init__(self)
self.pack()
self.master.title("Karlos")
self.button1 = Button( self, text = "CLICK HERE", width = 25,
command = self.new_window )
self.button1.grid( row = 0, column = 1, columnspan = 2, sticky = W+E+N+S )
def new_window(self):
self.newWindow = karl2()
class karl2(Frame):
def __init__(self):
new =tk.Frame.__init__(self)
new = Toplevel(self)
new.title("karlos More Window")
new.button = tk.Button( text = "PRESS TO CLOSE", width = 25,
command = self.close_window )
new.button.pack()
def close_window(self):
self.destroy()
def main():
karl().mainloop()
if __name__ == '__main__':
main()
Comments
Post a Comment