我正在尝试在path_entry字段中设置值。这是代码:
add_filter( 'woocommerce_marketing_menu_items', 'remove_wc_marketing_menu_item' );
function remove_wc_marketing_menu_item( $marketing_pages ) {
return array();
}
因此,当您单击浏览按钮时,它将打开一个窗口以在该选项卡的特定条目中加载文件路径。但是当我希望每个选项卡充当一个单独的实体时,我在所有其他选项卡的“输入”字段中注意到了相同的文件/值。 简而言之,我想为每个4个选项卡加载四个不同文件的文件路径。怎么可能
答案 0 :(得分:2)
这是因为您对所有条目都使用了相同的StringVar
self.file_path
。您应该为每个条目分别使用StringVar
:
from tkinter.scrolledtext import ScrolledText
...
def create_notebook(self, names):
nb = MyNotebook(self, names)
#self.file_path = tk.StringVar()
nb.pack()
def path_label(parent):
label = ttk.Label(parent, text="Person file: ")
label.grid(column=0, row=2, padx=10, pady=10)
return label
def path_entry(parent):
path = ttk.Entry(parent, width=30, textvariable=parent.file_path)
path.grid(column=1, row=2)
return path
def browse_button(parent):
browse = ttk.Button(parent, text="Browse", command=lambda:read_csv(parent), width=30)
browse.grid(column=1, row=3, padx=10, pady=10)
return browse
def newapp_label(parent):
label = ttk.Label(parent, text="Press to : ")
label.grid(column=0, row=4, padx=10, pady=10)
return label
def newapp_button(parent):
newapp = ttk.Button(parent, text="Run New Application", width=30)
newapp.grid(column=1, row=4, padx=10, pady=10)
return newapp
def add_scrolledtext(parent):
text = ScrolledText(parent, width=40, height=20)
text.grid(column=0, row=5, columnspan=2, padx=10, pady=10, sticky='nsew')
return text
def read_csv(parent):
file = filedialog.askopenfilename()
if file:
parent.file_path.set(file)
# Add some labels to each tab
for name in names:
tab = nb.tabs[name]
# create a StringVar for this tab
tab.file_path = tk.StringVar()
path_label(tab)
path_entry(tab)
browse_button(tab)
newapp_label(tab)
newapp_button(tab)
add_scrolledtext(tab)
return nb
此外,您也不应该编写类似label = ttk.Label(...).grid(...)
的内容,因为label
是grid(...)
的结果,即None
。将这两个函数分成两个语句:
label = ttk.Label(...)
label.grid(...)
更新:在每个标签中添加了ScrolledText
。