Python Assignment Using Tkinter
Python Assignment Using Tkinter
● attributes
● behavior
The self keyword is used to represent an instance (object) of the given class.
In this case, the two Cat objects cat1 and cat2 have their own name and age
attributes. If there was no self argument, the same class couldn't hold the
information for both these objects.
However, since the class is just a blueprint, self allows access to the
attributes and methods of each object in python. This allows each object to
have its own attributes and methods. Thus, even long before creating these
objects, we reference the objects as self while defining the class.
The class can be defined as a collection of objects. It is a logical entity that has some
specific attributes and methods. For example: if you have an employee class, then it
should contain an attribute and method, i.e. an email id, name, age, salary, etc.
Example:-
1. class ClassName:
2. <statement-1>
3. .
4. .
5. <statement-N>
Object:-
The object is an entity that has state and behavior. It may be any real-world object like
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the docstring defined in the
When we define a class, it needs to create an object to allocate the memory. Consider
Example:-
1. class car:
2. def __init__(self,modelname, year):
3. self.modelname = modelname
4. self.year = year
5. def display(self):
6. print(self.modelname,self.year)
7.
8. c1 = car("Toyota", 2016)
9. c1.display()
5.What is Inheritance
Ans:-
Inheritance is a powerful feature in object oriented programming.
It refers to defining a new class with little or no modification to an existing
class. The new class is called derived (or child) class and the one from which
it inherits is called the base (or parent) class.
A polygon is a closed figure with 3 or more sides. Say, we have a class called
Polygon defined as follows.
class Polygon:
def __init__(self, no_of_sides):
self.n = no_of_sides
self.sides = [0 for i in range(no_of_sides)]
def inputSides(self):
self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in
range(self.n)]
def dispSides(self):
for i in range(self.n):
print("Side",i+1,"is",self.sides[i])
Example:-
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36)
print(p1.name)
print(p1.age)
the del keyword can also be used to delete variables, lists, or parts of a list etc.
Example:-
x = "hello"
del x
print(x)
Multiple Inheritance :-If a class is able to be created from multiple base classes, this
kind of Inheritance is known as multiple Inheritance. When there is multiple Inheritance,
each of the attributes that are present in the classes of the base has been passed on to
the class that is derived from it.
Multilevel inheritance,:- the features that are part of the original class, as well as the
class that is derived from it, are passed on to the new class. It is similar to a relationship
involving grandparents and children.
Hierarchical Inheritance:- If multiple derived classes are created from the same base,
this kind of Inheritance is known as hierarchical inheritance. In this instance, we have
two base classes as a parent (base) class as well as two children (derived) classes.
Overriding is a very important part of OOP since it makes inheritance utilize its full
power. By using method overriding a class may "copy" another class, avoiding
duplicated code, and at the same time enhance or customize part of it. Method
with the same name of a method in the parent class. When you define a method in
the object you make this latter able to satisfy that method call, so the
class Parent(object):
def __init__(self):
self.value = 4
def get_value(self):
return self.value
class Child(Parent):
def get_value(self):
return self.value + 1
● The arguments are given in the super() function and the arguments in the
function that we have called should match.
● Every occurrence of the method that we are using should include the super()
keyword after we use it.
● We have to specify the class and methods present in it, which are referred to by
the super() function.
● We can use the super() function with the single inheritance and multiple
inheritances.
class Parent:
def __init__(self, txt):
self.message = txt
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x.printmessage()
An object-oriented paradigm is to design the program using classes and objects. The
object is related to real-word entities such as book, house, pencil, etc. The oops concept
focuses on writing the reusable code. It is a widespread technique to solve the problem
by creating objects.
Major principles of object-oriented programming system are given below.
● Class
● Object
● Method
● Inheritance
● Polymorphism
● Data Abstraction
● Encapsulation
Assignment-2
Creating a GUI application using Tkinter is an easy task. All you need to do is
application.
○ Enter the main event loop to take action against each event triggered
by the user.
2. What is the use Tkinter Module.
Ans:-
Radio Button:-
1. from tkinter import *
2.
3. def selection():
4. selection = "You selected the option " + str(radio.get())
5. label.config(text = selection)
6.
7. top = Tk()
8. top.geometry("300x150")
9. radio = IntVar()
10. lbl = Label(text = "Favourite programming language:")
11. lbl.pack()
12. R1 = Radiobutton(top, text="C", variable=radio, value=1,
13. command=selection)
14. R1.pack( anchor = W )
15.
16. R2 = Radiobutton(top, text="C++", variable=radio, value=2,
17. command=selection)
18. R2.pack( anchor = W )
19.
20. R3 = Radiobutton(top, text="Java", variable=radio, value=3,
21. command=selection)
22. R3.pack( anchor = W)
23.
24. label = Label(top)
25. label.pack()
26. top.mainloop()
LABEL:-
1. from tkinter.
2. import * a = Tk()
3. a.geometry("400x400")
4. a.title("test")
5. label = Label(a, text = "c# corner", bg = "green"\, bd = 100, fg = "white", font =
"Castellar")
6. label.pack()
7. a.mainloop()
List Box
from tkinter import *
top = Tk()
listbox = Listbox(top, height = 10,
width = 15,
bg = "grey",
activestyle = 'dotbox',
font = "Helvetica",
fg = "yellow")
top.geometry("300x250")
label = Label(top, text = " FOOD ITEMS")
listbox.insert(1, "Nachos")
listbox.insert(2, "Sandwich")
listbox.insert(3, "Burger")
listbox.insert(4, "Pizza")
listbox.insert(5, "Burrito")
label.pack()
listbox.pack()
top.mainloop()
Entry;-
import tkinter as tk
root = tk.Tk()
root.title("First Tkinter Window")
root.mainloop()
Check Box:-
from tkinter import *
root = Tk()
root.geometry("300x200")
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
Button1.pack()
Button2.pack()
Button3.pack()
mainloop()
Spinner:-
current_value = tk.StringVar(value=0)
spin_box = ttk.Spinbox(
container,
from_=0,
to=30,
textvariable=current_value,
wrap=True)
1 It is used only when we have one It is used only when we have one or many
named <input>, and the type named <input>, and the type attribute is
Syntax: Syntax:
checked>
3 It is represented as small circle It is represented as a small square box on
4 There are two states in the Radio There are three states in the Checkbox.
button. It can be either true or These areas are Checked, unchecked &
false. indeterminate.
5 It is used to limit the user's It is used when you want to allow the
choice to select only one option user to select more than one option or
from the list of the options none of the options from the list of
Parameters
The two options that can be used are default and parent.
1. default
The default option is used to mention the types of the default button, i.e. ABORT, RETRY,
or IGNORE in the message box.
2. parent
The parent option specifies the parent window on top of which, the message box is to
be displayed.