I am trying to read.xlsx worksheets using tkinter. But code is posting error of "TypeError: 'NoneType' object is not subscribable". I have made enough trial on it but not able to understand the issue with the code.
import openpyxl
from tkinter import filedialog
from tkinter import *
class data:
def __init__(self,master):
self.master = master
master.title("App")
master.geometry("600x200")
master.resizable(0,0)
x,y = 30,20
self.options = ['A',"B"]
self.om_variable = StringVar()
# self.om_variable.set(self.options[0])
# self.om_variable.trace('w', self.option_select)
self.om = OptionMenu(self.master, self.om_variable, *self.options).place(x = x+300,y = y+80)
Brw_2 = Button(master = self.master, text = 'Browse', width = 6, command=self.update_option_menu).place(x = x+500,y = y+40)
def update_option_menu(self):
self.om_variable.set('')
self.om["menu"].delete(0, "end")
xyx = self.get_SheetsName(r"C:\_Code\Inputs\SomeExcelFile.xlsx")
print("---->",xyx)
for string in xyx:
self.om["menu"].add_command(label=string,
command=lambda value=string: self.om_variable.set(value))
def get_SheetsName(self, excel_file):
wb = openpyxl.load_workbook(excel_file)
SH_lst = wb.sheetnames
Sh_names = [sh for sh in SH_lst if "AllTags" in sh]
return Sh_names
root = Tk()
myGui = data(root)
root.mainloop()
This is a common mistake. When you wrote this:
self.om = OptionMenu(self.master, self.om_variable, *self.options).place(x = x+300,y = y+80)
Python created an optionmenu and then called the .place()
method. The result of that which is None
is saved to self.om
. What you want is for the optionmenu to be created then stored in self.om
then placed using .place()
So you basically python interpreted it like this:
temp_var = OptionMenu(self.master, self.om_variable, *self.options)
self.om = temp_var .place(x = x+300,y = y+80)
del temp_var
What you should have written is:
self.om = OptionMenu(self.master, self.om_variable, *self.options)
self.om.place(x = x+300,y = y+80)
Tip: never directly call grid
/ place
/ pack
after calling the constructor without saving the object to a variable.
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.