Professional Documents
Culture Documents
DHULIKHEL, KAVRE
DEPARTMENT OF GEOMATICS ENGINEERING
Python Programming
Lab Report No: 7
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")
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')
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()
v = Scrollbar(root)
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()
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:
Syntax
from tkinter import *
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.