【问题标题】:How to display tab widget after a toolbar menu in Python?如何在 Python 中的工具栏菜单后显示选项卡小部件?
【发布时间】:2019-10-23 02:57:10
【问题描述】:

我是这个 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

【问题讨论】:

  • 如果你使用variable = Widget(...).pack(),那么你将None分配给variable,因为pack()/grid()/place()总是返回None。如果你不在其他地方使用这个variable,那么你可以跳过variable = ,只使用Widget(...).pack()。但如果你需要这个变量,那么你必须分两行完成variable = Widget()variable.pack()
  • @furas 我不确定。但我确实测试了这两种情况,它对我有用????

标签: python python-3.x tkinter widget tkinter-menu


【解决方案1】:

您将错误的父级设置为框架tabControl

变化:

tabControl = ttk.Notebook(menubar)

到:

tabControl = ttk.Notebook(root)

【讨论】:

  • 成功了!我很遗憾没有注意到?。谢谢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
相关资源
最近更新 更多