You are on page 1of 8

KATHMANDU UNIVERSITY

DHULIKHEL, KAVRE
DEPARTMENT OF GEOMATICS ENGINEERING

Python Programming
Lab Report No: 7

Submitted By: Submitted To:

Name: Sharwan Kumar Yadav Mr. Uma Shankar Panday

Roll No: 27 Department of Geomatics Engineering

Date of Submission: 6/16/2021


Q) Write a program that creates a GUI with a single button. When the button is pressed it should
create a second button. When that button is pressed, it should create a label that says, “Nice
job!”.

Syntax
from tkinter import *

def text():
w = Label(root, text="Nice job!!" ,bg='red', fg='blue',relief= 'groove')
w.pack()

def button():
b1 = Button(text="2nd button", command=text)
b1.pack()

root = Tk()
b = Button(text="1st Button", command=button)
b.pack()
root.mainloop()

Output:

Q) Python program to demonstrate two labels one image and other text.

Syntax:
import tkinter as tk

root = tk.Tk()
logo = tk.PhotoImage(file="command_line.gif")

w1 = tk.Label(root, image=logo).pack(side="left")

explanation = """At present, only GIF and PPM/PGM


formats are supported, but an interface
exists to allow additional image file
formats to be added easily."""

w2 = tk.Label(root,
justify=tk.LEFT,
padx = 10,
text=explanation).pack(side="right")
root.mainloop()

Output:

Description:
In this program the tkinter module, containing the tk toolkit has been imported. In order to
initialize tkinter, Tk root widget is created, which is a window with title bar and other decoration
provided by the window manager. After that the Label widget is created, the first parameter of
the Label call is the name of the parent window, in our case "root". So our Label widget is a
child of the root widget. The keyword parameter "text" specifies the text to be shown: The pack
method tells Tk to fit the size of the window to the given text. The window won't appear until we
enter the Tkinter event loop. Our script will remain in the event loop until we close the window.

Q) Checkbox Example:
Syntax:
from tkinter import*

window = Tk()
window.title('My Window')
window.geometry('300x300')

l = Label(window, bg='white',fg='purple', width=20, text='Choose your opinion!')


l.pack()
def print_selection():
if (var1.get() == 1) & (var2.get() == 0):
l.config(text='I love Python ')
elif (var1.get() == 0) & (var2.get() == 1):
l.config(text='I love C++')
elif (var1.get() == 0) & (var2.get() == 0):
l.config(text='I do not like anything')
else:
l.config(text='I love both')

var1 = IntVar()
var2 = IntVar()
c1 = Checkbutton(window, text='Python',variable=var1, onvalue=1, offvalue=0,
command=print_selection)
c1.pack()
c2 = Checkbutton(window, text='C++',variable=var2, onvalue=1, offvalue=0,
command=print_selection)
c2.pack()

window.mainloop()

Output:

Description:
Checkboxes, also known as tickboxes or tick boxes or check boxes, are widgets that permit the
user to make multiple selections from a number of different options. This is different to a radio
button, where the user can make only one choice. Usually, checkboxes are shown on the screen
as square boxes that can contain white spaces (for false, i.e not checked) or a tick mark or X (for
true, i.e., checked). A caption describing the meaning of the checkbox is usually shown adjacent
to the checkbox. The state of a checkbox is changed by clicking the mouse on the box.
Alternatively, it can be done by clicking on the caption, or by using a keyboard shortcut, for
example the space bar. A Checkbox has two states: on or off.
Q) Creating ScrollBar:

Syntax:
from tkinter import *

class ScrollBar:

def __init__(self):
root = Tk()

h = Scrollbar(root, orient = 'horizontal')

h.pack(side = BOTTOM, fill = X)

v = Scrollbar(root)

v.pack(side = RIGHT, fill = Y)

t = Text(root, width = 15, height = 15, wrap = NONE,


xscrollcommand = h.set,
yscrollcommand = v.set)

for i in range(20):
t.insert(END,"this is some text for ScrollBar\n")
t.pack(side=TOP, fill=X)

h.config(command=t.xview)
v.config(command=t.yview)

root.mainloop()

# create an object to Scrollbar class


s = ScrollBar()

Output:
Q) Dialogue and Message Box:

Syntax:
from tkinter import *
from tkinter import messagebox
#from tkMessageBox import *
def answer():
messagebox.showerror("Answer","Sorry no answer available")

def callback():
if messagebox.askyesno('Verify', 'Really Quit?'):
messagebox.showwarning('Yes', 'Not yet implemented')
else:
messagebox.showinfo('No', 'Quit has been cancelled')
Button(text='Quit', command=callback).pack(fill=X)
Button(text='Answer', command=answer).pack(fill=X)
mainloop()

Output:
Q) Open File Dialogue:

Syntax:
from tkinter import *
#from tkfiledialog import askopenfilename
from tkinter import filedialog
root = Tk()
def callback():
name = filedialog.askopenfilename()
print(name)
image = PhotoImage(file = name)
ll = Label(root, image=image)
ll.image= image
ll.pack()

errmsg = 'Error!'
Button(root, text= 'File Open', command=callback).pack(fill=X)
mainloop()

Output:

Q) Python program to create color chooser dialog box.

Syntax
from tkinter import *

from tkinter import colorchooser


def choose_color():
color_code = colorchooser.askcolor(title ="Choose color")
print(color_code)

root = Tk()
button = Button(root, text = "Select color",
command = choose_color)
button.pack()
root.geometry("300x300")
root.mainloop()

Output:

Description:
In the above program, the tkinter module is imported. The tkinter module has a package in it
named colorchooser, also imported. This package of the tkinter module helps in developing
the color chooser dialog box. This package has a function named  askcolor() that plays a major
role. The function helps in creating a color chooser dialog box. As soon as the function is
called, it makes the color chooser dialogue box pop up. The function returns the hexadecimal
code of the color selected by the user.

You might also like