Search code examples
pythonpython-3.xtkinterwidgettkinter-menu

How to display tab widget after a toolbar menu in Python?


I am new to this tkinter menu. I am trying to upload and display an excel file by using the 'filemenu' in the menubar and 'btnNew' in the toolbar menu.

The application did run but it does not display my excel file after I browse the file. The application only display the datatypes of each variable in the excel file.

import pandas as pd
import xlrd
import tkinter as tk

from tkinter import  Frame, Menu, Button, Label, Canvas
from tkinter import LEFT, RIGHT, TOP, BOTTOM, X, FLAT, RAISED
from tkinter import filedialog
from tkinter import ttk

root = tk.Tk() #main method 1 - create main window (parent window)
root.title("Data Visualisation")
width = 1000
height = 500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry('%dx%d+%d+%d' % (width, height, x, y))
root.resizable(1,1)

def browseFile():
    global workbook, copyWorkbook, excel_file, sheetName, worksheet

    fileName = filedialog.askopenfilename(initialdir = '/', title = 'New File', filetypes = (('excel file', '.xlsx'), ('excel file', '.xls'), ('all files', '*.*')))
    excel_file = pd.ExcelFile(fileName)
    workbook = xlrd.open_workbook(fileName)
    sheetCount = workbook.nsheets

    #Create tabs
    sheetName = []
    tab = []

    for x in range(workbook.nsheets):
        tab.append(ttk.Frame(tabControl))
        sheetName = workbook.sheet_names()
        tabControl.add(tab[x], text = sheetName[x])
        df_table = excel_file.parse(sheetName[x])
        print(df_table.dtypes)
        lblTable = Label(tab[x], text = df_table.to_string(index = False)).grid()
        btnGraph = Button(tab[x], text = "Graph").grid(sticky = 'w', column = 1, row = 5)

##MENU BAR
menubar = Menu(root)
root.config(menu = menubar)

#FILE MENU
filemenu = Menu(menubar, bg = '#BFBFBF', tearoff = 0)
menubar.add_cascade(label = 'File', menu = filemenu)

filemenu.add_command(label = 'New', compound = LEFT, command = browseFile)
filemenu.add_command(label = 'Open...', compound = LEFT)
filemenu.add_separator()
filemenu.add_command(label = 'Quit', compound = LEFT, command = root.quit)

#SEARCH MENU
searchmenu = Menu(menubar, bg = '#BFBFBF')
menubar.add_cascade(label = 'Search', menu = searchmenu)
searchmenu.add_command(label = 'Find...', compound = LEFT)
searchmenu.add_command(label = 'Replace...', compound = LEFT)

#HELP MENU
helpmenu = Menu(menubar, bg = '#BFBFBF')
menubar.add_cascade(label = 'Help', menu = helpmenu)
helpmenu.add_command(label = 'About', compound = LEFT)

##TOOLBAR MENU
toolbar = Frame(root, bd = 1, relief = RAISED)

#To browse excel file
btnNew = Button(toolbar, text = 'New', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 20, pady = 2, command = browseFile).pack(side = LEFT)
btnOpen = Button(toolbar, text = 'Open', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2).pack(side = LEFT)
btnFind = Button(toolbar, text = 'Find', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2).pack(side = LEFT)
btnReplace = Button(toolbar, text = 'Replace', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2).pack(side = LEFT)
btnQuit = Button(toolbar, text = 'Quit', compound = TOP, relief = FLAT, activebackground = '#ADD8E6', padx = 10, pady = 2, command = root.quit).pack(side = RIGHT)
toolbar.pack(side = TOP, fill = X)

###Tab Widget
tabControl = ttk.Notebook(menubar)
tabHome = ttk.Frame(tabControl)
tabControl.add(tabHome, text = "Home")

#All the automation Tab is pack here
tabControl.pack(expand = 1, fill = 'both', side = LEFT)

root.mainloop() #main method 2 - run the application

The application should display tabs and in each tab, it should contain each sheet from the excel file. All the tabs must be display after the toolbar menu.

I'm not sure what is the problem as is does not specify if there is any error in my code.

All feedback is welcomed as I am trying to learn how to make a better interface using python. Thank you for your help :D


Solution

  • You set the wrong parent to the frame tabControl.

    Change:

    tabControl = ttk.Notebook(menubar)
    

    to:

    tabControl = ttk.Notebook(root)