path_entryフィールドに値を設定しようとしています。これがコードです:
from tkinter import filedialog, ttk,scrolledtext
import tkinter as tk
class GUI(tk.Tk):
def __init__(self):
super().__init__()
self.title("SC")
self.geometry('375x600')
self.resizable(width=False, height=False)
names = ['Tab 1', 'Tab 2', 'Tab 3', 'Tab 4']
self.nb = self.create_notebook(names)
self.menu = self.create_menus()
self.mainloop()
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: ").grid(column=0, row=2, padx=10, pady=10)
return label
def path_entry(parent):
path = ttk.Entry(parent, width=30, textvariable=self.file_path).grid(column=1, row=2)
return path
def browse_button(parent):
browse = ttk.Button(parent, text="Browse", command=read_csv, width=30).grid(column=1, row=3, padx=10,
pady=10)
return browse
def newapp_label(parent):
label = ttk.Label(parent, text="Press to : ").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).grid(column=1, row=4, padx=10, pady=10)
return newapp
def read_csv():
file = filedialog.askopenfilename()
if file != None:
self.file_path.set(file)
# Add some labels to each tab
tabs = [nb.tabs['Tab 1'],nb.tabs['Tab 2'], nb.tabs['Tab 3'], nb.tabs['Tab 4']]
for tab in tabs:
path_label(tab)
path_entry(tab)
browse_button(tab)
newapp_label(tab)
newapp_button(tab)
return nb
def create_menus(self):
menu = tk.Menu(self, tearoff=False)
self.config(menu=menu)
subMenu = tk.Menu(menu, tearoff=False)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_separator()
subMenu.add_command(label='Exit', command=self.destroy)
return menu
class MyNotebook(ttk.Notebook):
''' A customised Notebook that remembers its tabs in a dictionary '''
def __init__(self, master, names):
super().__init__(master, width=390, height=470)
# Create tabs & save them by name in a dictionary
self.tabs = {}
for name in names:
self.tabs[name] = tab = ttk.Frame(self)
self.add(tab, text=name)
GUI()
したがって、参照ボタンをクリックすると、そのタブの特定のエントリにファイルパスをロードするウィンドウが開きます。しかし、各タブを個別のエンティティとして機能させたいのですが、他のすべてのタブの[エントリ]フィールドに同じファイル/値があることに気付きました。つまり、4つのタブごとに4つの異なるファイルのファイルパスをロードします。どうしてそれが可能か
これは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
。2つの関数を2つのステートメントに分離します。
label = ttk.Label(...)
label.grid(...)
更新:ScrolledText
各タブに追加されました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加