Day-8: GUI
29/06/2018
Department Of
Computer Science
Engineering
SKFGI, Mankundu
, Python 3 - GUI Programming (Tkinter)
Python provides various options for developing graphical user interfaces
(GUIs). The most important features are listed below.
Tkinter − Tkinter is the Python interface to the Tk GUI toolkit
shipped with Python. We would look this option in this chapter.
wxPython − WxPython brings the wxWidgets cross-platform GUI
library from its native C++ to Python. WxPython is a slightly more
modern approach to, which looks a little more native than Tkinter
across different operating systems as it does not attempt to create
its own set of widgets (although these can be themed to look much
like native components). It's fairly easy to get started with as well,
and has a growing developer community. You may need to bundle
wxPython with your applications, as it is not automatically installed
with Python.
PyQt −This is also a Python interface for a popular cross-platform
Qt GUI library.
JPython − JPython is a Python port for Java, which gives Python
scripts seamless access to the Java class libraries on the local
machine
1.Tkinter Programming
Tkinter is the standard GUI library for Python. Python when combined
with Tkinter provides a fast and easy way to create GUI applications.
Tkinter provides a powerful object-oriented interface to the Tk GUI
toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to
do is perform the following steps −
Import the Tkinter module.
Create the GUI application main window.
Add one or more of widgets to the GUI application.
, Enter the main event loop to take action against each event triggered by
the user.
Example
From tkinter import *
# note that module name has changed from Tkinter in Python 2 to
tkinter in Python 3
top = Tk()
# Code to add widgets will go here...
top.mainloop()
The last line calls the mainloop function. This function calls the endless
loop of the window, so the window will wait for any user interaction till we
close it.
If you forget to call the mainloop function, nothing will appear to the
user.
1.1To rename the widgets
from tkinter import *
window = Tk()
window.title("Welcome ")
window.mainloop()
1.2Setting Window Size
We can set the default window size using the geometry function like
this:
window.geometry('350x200')
The above line sets the window width to 350 pixels and the height
to 200 pixels.
1.3Print the screen size
from tkinter import*
root=Tk()
w=root.winfo_screenwidth()
h=root.winfo_screenheight()
print(w)
print(h)
29/06/2018
Department Of
Computer Science
Engineering
SKFGI, Mankundu
, Python 3 - GUI Programming (Tkinter)
Python provides various options for developing graphical user interfaces
(GUIs). The most important features are listed below.
Tkinter − Tkinter is the Python interface to the Tk GUI toolkit
shipped with Python. We would look this option in this chapter.
wxPython − WxPython brings the wxWidgets cross-platform GUI
library from its native C++ to Python. WxPython is a slightly more
modern approach to, which looks a little more native than Tkinter
across different operating systems as it does not attempt to create
its own set of widgets (although these can be themed to look much
like native components). It's fairly easy to get started with as well,
and has a growing developer community. You may need to bundle
wxPython with your applications, as it is not automatically installed
with Python.
PyQt −This is also a Python interface for a popular cross-platform
Qt GUI library.
JPython − JPython is a Python port for Java, which gives Python
scripts seamless access to the Java class libraries on the local
machine
1.Tkinter Programming
Tkinter is the standard GUI library for Python. Python when combined
with Tkinter provides a fast and easy way to create GUI applications.
Tkinter provides a powerful object-oriented interface to the Tk GUI
toolkit.
Creating a GUI application using Tkinter is an easy task. All you need to
do is perform the following steps −
Import the Tkinter module.
Create the GUI application main window.
Add one or more of widgets to the GUI application.
, Enter the main event loop to take action against each event triggered by
the user.
Example
From tkinter import *
# note that module name has changed from Tkinter in Python 2 to
tkinter in Python 3
top = Tk()
# Code to add widgets will go here...
top.mainloop()
The last line calls the mainloop function. This function calls the endless
loop of the window, so the window will wait for any user interaction till we
close it.
If you forget to call the mainloop function, nothing will appear to the
user.
1.1To rename the widgets
from tkinter import *
window = Tk()
window.title("Welcome ")
window.mainloop()
1.2Setting Window Size
We can set the default window size using the geometry function like
this:
window.geometry('350x200')
The above line sets the window width to 350 pixels and the height
to 200 pixels.
1.3Print the screen size
from tkinter import*
root=Tk()
w=root.winfo_screenwidth()
h=root.winfo_screenheight()
print(w)
print(h)