0

我是这个 tkinter 菜单的新手。我正在尝试通过使用菜单栏中的“ filemenu ”和工具栏菜单中的“ btnNew ”来上传和显示 excel 文件。

该应用程序确实运行了,但在我浏览文件后它不显示我的 Excel 文件。应用程序仅显示 excel 文件中每个变量的数据类型。

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

应用程序应显示选项卡,并且在每个选项卡中,它应包含 excel 文件中的每个工作表。所有选项卡都必须显示在工具栏菜单之后。

我不确定是什么问题,因为没有指定我的代码中是否有任何错误。

欢迎所有反馈,因为我正在尝试学习如何使用 python 制作更好的界面。谢谢你的帮助:D

4

1 回答 1

0

您将错误的父级设置为 frame tabControl

改变:

tabControl = ttk.Notebook(menubar)

到:

tabControl = ttk.Notebook(root)
于 2019-10-23T03:04:36.973 回答