简体   繁体   中英

Resize treeview widget in tkinter

I'm new to tkinter gui framework of python

I have a doubt in treeview widget of the tkinter gui

my doubt is how to resize the treeview widget so that i can place the two treeview widget aside photos attached below and i doing a project on mysql database so for the frontend i used tkinter gui

Screenshot of output

from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk 
import os 
import pickle 
import mysql.connector  as sql
from tkinter import messagebox


def click_home():
    """set to default home tab where details like no. of students, employees, department shows """

    home_frame = Frame(root)
    home_frame.place(x=120, y=105, height=440, width=910)

    home_dashboard_fram_r=Image.open('files\home_new.png')
    home_dashboard_fram_r=home_dashboard_fram_r.resize((910,440),Image.ANTIALIAS)
    home_dashboard_frame = ImageTk.PhotoImage(home_dashboard_fram_r)
    home_panel = Label(home_frame, image=home_dashboard_frame, bg="white")
    home_panel.image= home_dashboard_frame
    home_panel.pack(fill='both', expand='yes')

def click_manage():
        """ opens new frame from where one can go to manage students, employees, departments, course, section
        and batch"""

        manage_frame = Frame(root, bg="white")
        manage_frame.place(x=120, y=105, height=440, width=910)
        manage_dashboard_frame_r = Image.open('files\\manage_frame1.png')
        manage_dashboard_frame_r=manage_dashboard_frame_r.resize((910,440),Image.ANTIALIAS)
        manage_dashboard_frame = ImageTk.PhotoImage(manage_dashboard_frame_r)
        manage_panel = Label(manage_frame, image=manage_dashboard_frame, bg="white")
        manage_panel.image = manage_dashboard_frame
        manage_panel.pack(fill='both', expand='yes')
        
        student_r=Image.open('Pics\\student.png')
        student_r=student_r.resize((100,100),Image.ANTIALIAS)
        student = ImageTk.PhotoImage(student_r)
        student_button = Button(manage_frame, image=student, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
        student_button.image = student
        student_button.place(x=120, y=115)

        employee_r=Image.open('Pics\\employee.png')
        employee_r=employee_r.resize((100,100),Image.ANTIALIAS)
        employee = ImageTk.PhotoImage(employee_r)
        employee_button = Button(manage_frame, image=employee, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
        employee_button.image =employee
        employee_button.place(x=395, y=115)

        department_r=Image.open('Pics\\department.png')
        department_r=department_r.resize((100,100),Image.ANTIALIAS)
        department = ImageTk.PhotoImage(department_r)
        department_button = Button(manage_frame, image=department, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
        department_button.image = department
        department_button.place(x=672, y=115)

        course_r=Image.open('Pics\\course.png')
        course_r=course_r.resize((100,100),Image.ANTIALIAS)
        course = ImageTk.PhotoImage(course_r)
        course_button = Button(manage_frame, image=course, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
        course_button.image = course
        course_button.place(x=121, y=300)

        section_r=Image.open('Pics\\section.png')
        section_r=section_r.resize((100,100),Image.ANTIALIAS)
        section = ImageTk.PhotoImage(section_r)
        section_button = Button(manage_frame, image=section, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
        section_button.image = section
        section_button.place(x=396, y=300)


        batch_r=Image.open('Pics\\batch.png')
        batch_r=batch_r.resize((100,100),Image.ANTIALIAS)
        batch = ImageTk.PhotoImage(batch_r)
        batch_button = Button(manage_frame, image=batch, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
        batch_button.image = batch
        batch_button.place(x=673, y=300)

def click_view():
    """ Displays partial data into tree view of students, employees, departments, courses when clicked view tab
    on interface """
    view_frame = Frame(root, bg="white")
    view_frame.place(x=120, y=105, height=440, width=910)

    view_dashboard_frame_r=Image.open('Pics\\view_frame.png')
    view_dashboard_frame_r=view_dashboard_frame_r.resize((910,440),Image.ANTIALIAS)
    view_dashboard_frame = ImageTk.PhotoImage(view_dashboard_frame_r)
    view_panel = Label(view_frame, image=view_dashboard_frame, bg="white")
    view_panel.image=view_dashboard_frame
    view_panel.pack(fill='both', expand='yes')

    department_view_label = Label(view_frame, text="View Department Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
    department_view_label.place(x=770, y=290)

    course_view_label = Label(view_frame, text="View Course Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
    course_view_label.place(x=170, y=290)



    
    # ========================================================================
    # ============================Displaying Student Information==============
    # ========================================================================

    student_view_label = Label(view_frame, text="View Students Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
    student_view_label.place(x=170, y=6)

    view_student_frame = Frame(view_frame, bg="white")
    view_student_frame.place(x=10, y=40, height=250, width=575)

    style = ttk.Style()
    style.configure("Treeview.Heading", font=('yu gothic ui', 10, "bold"), foreground="red")
    style.configure("Treeview", font=('yu gothic ui', 9, "bold"), foreground="#f29b0f")

    scroll_y = Scrollbar(view_student_frame, orient=VERTICAL)
    scroll_x = Scrollbar(view_student_frame, orient=HORIZONTAL)
    view_student_tree = ttk.Treeview(view_student_frame,columns=("STUDENT ID", "STUDENT NAME", "COURSE ENROLLED", "PHONE NO."),xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set,height=2)
    scroll_x.pack(side=BOTTOM, fill=X)
    scroll_y.pack(side=RIGHT, fill=Y)
    scroll_x.config(command=view_student_tree.xview)
    scroll_y.config(command=view_student_tree.yview)


    
    # ==========================TreeView Heading====================
    view_student_tree.heading("STUDENT ID", text="STUDENT ID")
    view_student_tree.heading("STUDENT NAME", text="STUDENT NAME")
    view_student_tree.heading("COURSE ENROLLED", text="COURSE ENROLLED")
    view_student_tree.heading("PHONE NO.", text="PHONE NO.")
    view_student_tree["show"] = "headings"

    # ==========================TreeView Column====================
    view_student_tree.column("STUDENT ID", width=50)
    view_student_tree.column("STUDENT NAME", width=50)
    view_student_tree.column("COURSE ENROLLED", width=50)
    view_student_tree.column("PHONE NO.", width=50)
    view_student_tree.pack(fill=BOTH, expand=1)

    
    # ========================================================================
    # =========================Displaying instructor Information==============
    # ========================================================================

    view_employee_frame = Frame(view_frame, bg="white")
    view_employee_frame.place(x=595, y=40, height=250, width=575)

    employee_view_label = Label(view_frame, text="View Employees Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
    employee_view_label.place(x=600, y=5)

    scroll_y_e = Scrollbar(view_employee_frame, orient=VERTICAL)
    scroll_x_e = Scrollbar(view_employee_frame, orient=HORIZONTAL)
    view_employee_tree = ttk.Treeview(view_employee_frame,columns=("EMPLOYEE ID", "EMPLOYEE NAME", "DEPARTMENT", "EMPLOYEE TYPE"),xscrollcommand=scroll_x_e.set, yscrollcommand=scroll_y_e.set)
    scroll_x_e.pack(side=BOTTOM, fill=X)
    scroll_y_e.pack(side=RIGHT, fill=Y)
    scroll_x_e.config(command=view_employee_tree.xview)
    scroll_y_e.config(command=view_employee_tree.yview)

    # ==========================TreeView Heading====================
    view_employee_tree.heading("EMPLOYEE ID", text="EMPLOYEE ID")
    view_employee_tree.heading("EMPLOYEE NAME", text="EMPLOYEE NAME")
    view_employee_tree.heading("DEPARTMENT", text="DEPARTMENT")
    view_employee_tree.heading("EMPLOYEE TYPE", text="EMPLOYEE TYPE")
    view_employee_tree["show"] = "headings"

    # ==========================TreeView Column====================
    view_employee_tree.column("EMPLOYEE ID", width=50)
    view_employee_tree.column("EMPLOYEE NAME", width=150)
    view_employee_tree.column("DEPARTMENT", width=100)
    view_employee_tree.column("EMPLOYEE TYPE", width=100)
    view_employee_tree.pack(fill=BOTH, expand=1)


root = Tk()
root.geometry("1067x600")
root.title("Dashboard")
root.resizable(False, False) 
#root.iconbitmap('images\\logo.ico')

bg = ImageTk.PhotoImage(file="files\Dashboard1.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)


home_r=Image.open('Pics\\home.png')
home_r=home_r.resize((50, 50), Image.ANTIALIAS)
home = ImageTk.PhotoImage(home_r)
home_button = Button(root, image=home,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2",command=click_home)
home_button.place(x=40, y=113)


manage_r=Image.open('Pics\\manage.png')
manage_r=manage_r.resize((50, 50), Image.ANTIALIAS)
manage = ImageTk.PhotoImage(manage_r)
manage_button = Button(root, image=manage,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2",command=click_manage)
manage_button.place(x=38, y=205)

view_r=Image.open('Pics\\view.png')
view_r=view_r.resize((50, 50), Image.ANTIALIAS)
view = ImageTk.PhotoImage(view_r)
view_button = Button(root, image=view,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2",command=click_view)
view_button.place(x=36, y=299)

setting_r = Image.open('Pics\\setting.png')
setting_r=setting_r.resize((50, 50), Image.ANTIALIAS)
setting = ImageTk.PhotoImage(setting_r)
setting_button = Button(root, image=setting,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2")
setting_button.place(x=38, y=395)

exit_1_r=Image.open('Pics\\exit_button.png')
exit_1_r=exit_1_r.resize((50, 50), Image.ANTIALIAS)
exit_1 = ImageTk.PhotoImage(exit_1_r)
exit_button = Button(root, image=exit_1,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2")
exit_button.place(x=37, y=490)

logout = ImageTk.PhotoImage(file='Pics\logout.png')
logout_button = Button(root, image=logout,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2")
logout_button.place(x=940, y=56)



root.mainloop()

Which arugument i should use to change the size of treeview widget..........

use expand= True, fill = y or x to resize it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM