ARoom Vacant

self.root = tk.Toplevel()
self.root.title("Room Vacant")
self.root.configure(bg="#585858")
self.root.resizable(width=0, height=0)
win_width = 1400
print(1280 * 56.25 / 100)
win_height = int(1280 * 56.25 / 100)
self.root.geometry(str(win_width) + "x" + str(win_height))
# Get screen size
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
Util.set_font_size(screen_width, screen_height, win_width, win_height)
self.width = win_width * 96 / 100
self.height = win_height * 94 / 100
After creation of window, we will add a canvas background.
bg_canvas = RoundBackgroundFrame(self.root, self.width, self.height, padding, cornerradius, self.color,
"#585858")
bg_canvas.place(width=self.width, height=self.height, x=win_width / 2 - self.width / 2,
y=win_height / 2 - self.height / 2)
class RoundBackgroundFrame(tk.Canvas):
def __init__(self, parent, width, height, padding, cornerradius, color, bg):
tk.Canvas.__init__(self, parent, borderwidth=0, relief="flat", highlightthickness=0, bg=bg)
self.width = width
self.height = height
self.padding = padding
self.cornerradius = cornerradius
self.color = color
original = Image.open("images/corner.png")
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_r_t = ImageTk.PhotoImage(resized)
original = original.rotate(90, expand=0)
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_l_t = ImageTk.PhotoImage(resized)
original = original.rotate(90, expand=0)
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_l_b = ImageTk.PhotoImage(resized)
original = original.rotate(90, expand=0)
resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
self.image_r_b = ImageTk.PhotoImage(resized)
self.shape(width, height, padding, cornerradius, color)
(x0, y0, x1, y1) = self.bbox("all")
width = (x1 - x0)
height = (y1 - y0)
self.configure(width=width, height=height)
def shape(self, width, height, padding, cornerradius, color):
self.create_polygon((padding, height - cornerradius - padding, padding, cornerradius + padding,
padding + cornerradius, padding, width - padding - cornerradius, padding,
width - padding, cornerradius + padding, width - padding,
height - cornerradius - padding, width - padding - cornerradius, height - padding,
padding + cornerradius, height - padding), fill=color, outline=color)
self.create_image(width - self.image_r_t.width(), 0, image=self.image_r_t, anchor=NW)
self.create_image(0, 0, image=self.image_l_t, anchor=NW)
self.create_image(0, height - self.image_l_b.width(), image=self.image_l_b, anchor=NW)
self.create_image(width - self.image_r_b.width(), height - self.image_r_b.width(), image=self.image_r_b, anchor=NW)
self.width = self.width * 98 / 100
self.height = self.height * 96 / 100
self.base_frame = Frame(self.root, width=self.width, height=self.height, bg=self.color)
bg_canvas.create_window(self.width / 100, self.height * 2 / 100, anchor=NW, window=self.base_frame)
# Add Heading Name
add_heading_label(self.base_frame, self.color, AppConstant.FONT_SIZE, self.height, self.width)
def add_heading_label(base_frame, color, font_size, height, width):
label_heading = Label(base_frame, text="Room Vacant", anchor=CENTER, bg=color,
font=("Lucida Grande", font_size + 6))
label_heading.place(width=width * 90 / 100, height=height * 5 / 100, x=width * 5 / 100, y=height * 0.2 / 100)
# Add line
add_line_border(self.base_frame, self.color, self.height, self.width)
def add_line_border(base_frame, color, height, width):
line_canvas = Canvas(base_frame, bg=color, borderwidth=0, relief="flat", highlightthickness=0)
line_canvas.place(width=width - 4, height=5, x=2, y=height * 7 / 100)
line_canvas.create_line(0, 0, width, 0, fill="#787878")
# Add Button Font
button_font = add_action_frame(self.base_frame, self.color, self.height, self.width)
def add_action_frame(base_frame, color, height, width):
afw = width * 0.35
afh = height * 0.91
button_font = ("Lucida Grande", AppConstant.FONT_SIZE - 6)
label_frame_action = LabelFrame(base_frame, text="Actions", font=button_font, pady=afw * 2 / 100,
padx=afw * 2 / 100, bg=color)
label_frame_action.place(width=afw, height=afh, x=width * 0.65, y=height * 0.09)
return button_font
# Add room search
add_room_search_frame(self.base_frame, button_font, self.color, self.height, self.width)
def add_room_search_frame(base_frame, button_font, color, height, width):
sfw = width * 0.638
sfh = height * 0.45
label_frame_search = LabelFrame(base_frame, text="Search Room", font=button_font, pady=sfw * 2 / 100,
padx=sfw * 2 / 100, bg=color)
label_frame_search.place(width=sfw, height=sfh, x=0, y=height * 0.09)
# Add Student search
add_student_search_frame(self.base_frame, button_font, self.color, self.height, self.width)
def add_student_search_frame(base_frame, button_font, color, height, width):
sfw = width * 0.638
sfh = height * 0.45
label_frame_search = LabelFrame(base_frame, text="Search Student", font=button_font, pady=sfw * 2 / 100,
padx=sfw * 2 / 100, bg=color)
label_frame_search.place(width=sfw, height=sfh, x=0, y=height * 0.55)
self.search_room_var = CustomEntry(self.base_frame, 300, 55, 10, 2, self.color, "Search Room",
"images/ic_search.png")
self.search_room_var.place(x=25, y=95)
self.search_room_var.entry.bind('', self.search_room)
Here's the logic to search Room in table
def search_room(self, *arg):
if self.search_book_var.entry.get() != "":
self.tree.delete(*self.tree.get_children())
conn = Util.connect_db()
cursor = conn.cursor()
status = "vacant"
cursor.execute("SELECT * FROM `room` WHERE status LIKE ? AND (`room_no` LIKE ? OR `room_rent` LIKE ?)",
(status, '%' + str(self.search_book_var.entry.get()) + '%',
'%' + str(self.search_book_var.entry.get()) + '%'))
sql_output = cursor.fetchall()
for data in sql_output:
self.tree.insert('', 'end', values=data, tags=('even',))
else:
self.tree.delete(*self.tree.get_children())
self.reset_room()
def reset_room(self):
self.get_room_data()
class Util:
@staticmethod
def connect_db():
db = None
try:
db = sql.connect("hostelmanagement.db")
return db
except sql.Error as error:
print("Failed to insert data into sqlite table", error)
Fetch data from database and show it in table form with the use of "Treeview" of Tkinter.
style = ttk.Style()
# style.element_create("Custom.Treeheading.border", "from", "default")
style.layout("Custom.Treeview.Heading", [
("Custom.Treeheading.cell", {'sticky': 'nswe'}),
("Custom.Treeheading.border", {'sticky': 'nswe', 'children': [
("Custom.Treeheading.padding", {'sticky': 'nswe', 'children': [
("Custom.Treeheading.image", {'side': 'right', 'sticky': ''}),
("Custom.Treeheading.text", {'sticky': 'we'})
]})
]}),
])
style.configure("Custom.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11), rowheight=30)
self.room_table_heading()
self.tree = ttk.Treeview(self.base_frame, height=4, columns=("c1", "c2"),
selectmode="extended", show='tree', style="Custom.Treeview")
self.tree.tag_configure('odd', background='#DFEBF6', foreground="#000000", )
self.tree.tag_configure('even', background='#FFFFFF', foreground="#000000", )
self.tree.place(x=20, y=195)
self.room_selected = False
self.selected_room_no = None
self.tree.bind('<>', self.on_select_room)
self.get_room_data()
def get_room_data(self):
db = Util.connect_db()
cursor = db.cursor()
status = "vacant"
cursor.execute("SELECT room_no, room_rent FROM room WHERE status = ?", (status,))
sql_output = cursor.fetchall()
vsby = ttk.Scrollbar(self.base_frame, orient="vertical", command=self.tree.yview)
vsby.place(x=275, y=195, height=120)
self.tree.configure(yscrollcommand=vsby.set)
self.tree.column("#0", width=0)
self.tree.column("#1", width=100)
self.tree.column("#2", width=150)
self.tree.column("#1", anchor=tk.CENTER)
self.tree.column("#2", anchor=tk.CENTER)
self.tree.heading("#1", text="Room Number", anchor=tk.CENTER)
self.tree.heading("#2", text="Price")
We have created our own heading for Room table, lets configure it.
def room_table_heading(self):
heading_y = 165
bg_color = "#2929ff"
canvas = Canvas(self.base_frame, width=250, height=30)
canvas.create_rectangle(0, 0, 250, 30, fill=bg_color)
canvas.place(x=20, y=heading_y - 2)
room_no_label = Label(self.base_frame, text="Room No",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
room_no_label.place(x=50, y=heading_y, width=100)
room_no_label.configure(anchor="center")
room_price_label = Label(self.base_frame, text="Price",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
room_price_label.place(x=152, y=heading_y, width=100)
room_price_label.configure(anchor="center")
We have created our own heading for Student table, lets configure it.
def student_table_heading(self):
heading_y = self.height * 0.695
bg_color = "#2929ff"
canvas = Canvas(self.base_frame, width=770, height=30)
canvas.create_rectangle(0, 0, 770, 30, fill=bg_color)
canvas.place(x=20, y=heading_y - 3)
student_id_label = Label(self.base_frame, text="Student Id",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
student_id_label.place(x=38, y=heading_y, width=110)
student_id_label.configure(anchor="center")
student_name_label = Label(self.base_frame, text="Name",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
student_name_label.place(x=160, y=heading_y, width=100)
student_name_label.configure(anchor="center")
student_contactno_label = Label(self.base_frame, text="Contact No",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
student_contactno_label.place(x=265, y=heading_y, width=180)
student_contactno_label.configure(anchor="center")
student_email = Label(self.base_frame, text="Email Id",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
student_email.place(x=440, y=heading_y, width=160)
student_email.configure(anchor="center")
student_adds_label = Label(self.base_frame, text="Address",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
student_adds_label.place(x=570, y=heading_y, width=160)
student_adds_label.configure(anchor=tk.E)
Now Create a Table for selected Student in the right side of the window.
self.selected_student_tree = ttk.Treeview(self.base_frame, height=1, show="tree")
self.selected_student_tree.place(x=self.width / 2 + 240, y=180)
self.selected_student_tree["columns"] = "1", "2", "3"
self.selected_student_tree.column("#0", width=0)
self.selected_student_tree.column("#1", width=70)
self.selected_student_tree.column("#2", width=200)
self.selected_student_tree.column("#3", width=100)
self.selected_student_tree.column("#1", anchor=tk.CENTER)
self.selected_student_tree.column("#2", anchor=tk.CENTER)
self.selected_student_tree.column("#3", anchor=tk.CENTER)
self.selected_book_table_heading()
self.root.attributes('-alpha', 0.0)
Util.center(self.root)
self.root.attributes('-alpha', 1.0)
We have created our own heading for Selected Student Table, lets configure it.
def selected_book_table_heading(self):
heading_y = 150
bg_color = "#2929ff"
canvas = Canvas(self.base_frame, width=375, height=30)
canvas.create_rectangle(0, 0, 375, 30, fill=bg_color)
canvas.place(x=self.width / 2 + 238, y=heading_y - 3)
book_id_label = Label(self.base_frame, text="Student Id",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
book_id_label.place(x=self.width / 2 + 240, y=heading_y, width=120)
book_id_label.configure(anchor="center")
book_name_label = Label(self.base_frame, text="Name",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
book_name_label.place(x=self.width / 2 + 380, y=heading_y, width=100)
book_name_label.configure(anchor="center")
fine_label = Label(self.base_frame, text="Rent",
font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
foreground="white")
fine_label.place(x=self.width / 2 + 535, y=heading_y, width=70)
fine_label.configure(anchor="center")
At last we will manage closing of window
def on_closing():
self.root.destroy()
args[0].deiconify()
self.root.protocol("WM_DELETE_WINDOW", on_closing)
Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2021 by Programmer Mitra. All Rights Reserved.