- 我正在制作一个Python tkinter应用程序。事情是,我不知道如何把我在第一个标签的所有剩余的笔记本标签。
- 完成此任务需要哪些步骤?你需要多少细节stackoverflow "看起来你的帖子主要是代码;请多补充一些细节。我不需要再补充什么了。我正在做一个简单的程序,需要一个简单的答案。
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import END, ANCHOR
# Define fonts and colors
text_color = "#fffacd"
menu_color = "#6c769b"
root_color = "#6c899a"
button_color = "#6c898a"
my_font = ("Times New Roman", 10)
# Define Window
root = tkinter.Tk()
root.title("IT2Go NotePad")
root.iconbitmap("pad.ico")
root.geometry('500x500')
root.config(bg=root_color)
# root.resizable(0, 0)
it2goNotebook = ttk.Notebook(root)
it2goNotebook.pack(pady=15)
# Create frame
docks = Frame(it2goNotebook, width=500, height=500, bg=root_color)
kbcombo = Frame(it2goNotebook, width=500, height=500, bg=menu_color)
monitors = Frame(it2goNotebook, width=500, height=500, bg=root_color)
webcams = Frame(it2goNotebook, width=500, height=500, bg=menu_color)
headsets = Frame(it2goNotebook, width=500, height=500, bg=root_color)
pucks = Frame(it2goNotebook, width=500, height=500, bg=menu_color)
docks.pack(fill="both", expand=1)
kbcombo.pack(fill="both", expand=1)
monitors.pack(fill="both", expand=1)
webcams.pack(fill="both", expand=1)
headsets.pack(fill="both", expand=1)
pucks.pack(fill="both", expand=1)
it2goNotebook.add(docks, text="Docks")
it2goNotebook.add(kbcombo, text="KB Combo")
it2goNotebook.add(monitors, text="Monitors")
it2goNotebook.add(webcams, text="Webcams")
it2goNotebook.add(headsets, text="Headsets")
it2goNotebook.add(pucks, text="Pucks")
# Define functions
def add_name():
"""Add name to list box"""
my_listbox.insert(END, list_entry.get())
list_entry.delete(0, END)
def remove_item():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox.delete(ANCHOR)
def save_list():
"""Save file to Text File"""
with open("docklist.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def open_list():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox.insert(END, line)
except:
return
# Define Docks Layout
input_frame = tkinter.Frame(docks, bg=menu_color)
output_frame = tkinter.Frame(docks, bg=menu_color)
button_frame = tkinter.Frame(docks, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input Docks layout
list_entry = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name)
output_frame.pack()
list_entry.grid(row=0, column=0)
list_add_button.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output Docks layout
my_scrollbar = tkinter.Scrollbar(output_frame)
my_listbox = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar.set)
# Link scrollbar to listbox
my_scrollbar.config(command=my_listbox.yview)
my_listbox.grid(row=0, column=0)
my_scrollbar.grid(row=0, column=1, sticky=NS)
# Button Docks Layout
remove_item_button = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item)
save_button = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list)
quit_button = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Open the previous list if available
open_list()
# Run the root windows's main loop
root.mainloop()
像这样:
列出所有制表符:
tabs = [docks, kbcombo, monitors, webcams, headsets, pucks]
现在你应该可以把剩下的放到for循环中了:
for tab in tabs:
# Define Docks Layout
input_frame = tkinter.Frame(tab, bg=menu_color)
output_frame = tkinter.Frame(tab, bg=menu_color)
button_frame = tkinter.Frame(tab, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input Docks layout
list_entry = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name)
output_frame.pack()
list_entry.grid(row=0, column=0)
list_add_button.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output Docks layout
my_scrollbar = tkinter.Scrollbar(output_frame)
my_listbox = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar.set)
# Link scrollbar to listbox
my_scrollbar.config(command=my_listbox.yview)
my_listbox.grid(row=0, column=0)
my_scrollbar.grid(row=0, column=1, sticky=NS)
# Button Docks Layout
remove_item_button = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item)
save_button = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list)
quit_button = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
请记住,这删除了一些应用程序的功能。你需要更新函数以接受参数(然后用适当的参数调用它们),因为对象如my_listbox
不存在于for循环迭代的范围之外。下面是一个例子:
def add_name(listbox, list_entry):
"""Add name to list box"""
listbox.insert(END, list_entry.get())
list_entry.delete(0, END)
你需要重新编写你的函数,使其像这样工作。此外,确保为每个选项卡创建不同的文件,这样列表就不会相互重叠。这也可以通过将正确的文件名传递给函数来实现。
我不明白....所以我没有使用For循环:
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import END, ANCHOR
# Define fonts and colors
text_color = "#fffacd"
menu_color = "#6c769b"
root_color = "#6c899a"
button_color = "#6c898a"
my_font = ("Times New Roman", 10)
# Define Window
root = tkinter.Tk()
root.title("IT2Go NotePad")
root.iconbitmap("pad.ico")
root.geometry('500x500')
root.config(bg=root_color)
# root.resizable(0, 0)
it2goNotebook = ttk.Notebook(root)
it2goNotebook.pack(pady=15)
# Create frame
docks = Frame(it2goNotebook, width=500, height=500, bg=root_color)
kbcombo = Frame(it2goNotebook, width=500, height=500, bg=menu_color)
monitors = Frame(it2goNotebook, width=500, height=500, bg=root_color)
webcams = Frame(it2goNotebook, width=500, height=500, bg=menu_color)
headsets = Frame(it2goNotebook, width=500, height=500, bg=root_color)
pucks = Frame(it2goNotebook, width=500, height=500, bg=menu_color)
docks.pack(fill="both", expand=1)
kbcombo.pack(fill="both", expand=1)
monitors.pack(fill="both", expand=1)
webcams.pack(fill="both", expand=1)
headsets.pack(fill="both", expand=1)
pucks.pack(fill="both", expand=1)
it2goNotebook.add(docks, text="Docks")
it2goNotebook.add(kbcombo, text="KB Combo")
it2goNotebook.add(monitors, text="Monitors")
it2goNotebook.add(webcams, text="Webcams")
it2goNotebook.add(headsets, text="Headsets")
it2goNotebook.add(pucks, text="Pucks")
# Define functions
def add_name1():
"""Add name to list box"""
my_listbox1.insert(END, list_entry1.get())
list_entry1.delete(0, END)
def add_name2():
"""Add name to list box"""
my_listbox2.insert(END, list_entry2.get())
list_entry2.delete(0, END)
def add_name3():
"""Add name to list box"""
my_listbox3.insert(END, list_entry3.get())
list_entry3.delete(0, END)
def add_name4():
"""Add name to list box"""
my_listbox4.insert(END, list_entry4.get())
list_entry4.delete(0, END)
def add_name5():
"""Add name to list box"""
my_listbox5.insert(END, list_entry5.get())
list_entry5.delete(0, END)
def add_name6():
"""Add name to list box"""
my_listbox6.insert(END, list_entry6.get())
list_entry6.delete(0, END)
def remove_item1():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox1.delete(ANCHOR)
def remove_item2():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox2.delete(ANCHOR)
def remove_item3():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox3.delete(ANCHOR)
def remove_item4():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox4.delete(ANCHOR)
def remove_item5():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox5.delete(ANCHOR)
def remove_item6():
"""Remove the selected (ANCHOR) item from the listbox"""
my_listbox6.delete(ANCHOR)
def save_list1():
"""Save file to Text File"""
with open("docklst.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox1.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def save_list2():
"""Save file to Text File"""
with open("kbcombolst.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox2.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def save_list3():
"""Save file to Text File"""
with open("monitorslst.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox3.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def save_list4():
"""Save file to Text File"""
with open("webcamslst.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox4.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def save_list5():
"""Save file to Text File"""
with open("headsetslst.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox5.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def save_list6():
"""Save file to Text File"""
with open("puckslst.txt", 'w') as f:
# listbox.get() returns a tuple
list_tuple = my_listbox6.get(0, END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
def open_list1():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox1.insert(END, line)
except:
return
def open_list2():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox2.insert(END, line)
except:
return
def open_list3():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox3.insert(END, line)
except:
return
def open_list4():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox4.insert(END, line)
except:
return
def open_list5():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox5.insert(END, line)
except:
return
def open_list6():
"""Open list if there is one upon starting"""
try:
with open("docklist.txt", 'r') as f:
for line in f:
my_listbox6.insert(END, line)
except:
return
# Define Docks Layout
input_frame = tkinter.Frame(docks, bg=menu_color)
output_frame = tkinter.Frame(docks, bg=menu_color)
button_frame = tkinter.Frame(docks, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input Docks layout
list_entry1 = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button1 = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name1)
output_frame.pack()
list_entry1.grid(row=0, column=0)
list_add_button1.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output Docks layout
my_scrollbar1 = tkinter.Scrollbar(output_frame)
my_listbox1 = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar1.set)
# Link scrollbar to listbox
my_scrollbar1.config(command=my_listbox1.yview)
my_listbox1.grid(row=0, column=0)
my_scrollbar1.grid(row=0, column=1, sticky=NS)
# Button Docks Layout
remove_item_button1 = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item1)
save_button1 = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list1)
quit_button1 = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button1.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button1.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button1.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Define kbcombo Layout
input_frame = tkinter.Frame(kbcombo, bg=menu_color)
output_frame = tkinter.Frame(kbcombo, bg=menu_color)
button_frame = tkinter.Frame(kbcombo, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input kbcombo layout
list_entry2 = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button2 = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name2)
output_frame.pack()
list_entry2.grid(row=0, column=0)
list_add_button2.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output kbcombo layout
my_scrollbar2 = tkinter.Scrollbar(output_frame)
my_listbox2 = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar2.set)
# Link scrollbar to listbox
my_scrollbar2.config(command=my_listbox2.yview)
my_listbox2.grid(row=0, column=0)
my_scrollbar2.grid(row=0, column=1, sticky=NS)
# Button kbcombo Layout
remove_item_button2 = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item2)
save_button2 = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list2)
quit_button2 = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button2.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button2.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button2.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Define monitors Layout
input_frame = tkinter.Frame(monitors, bg=menu_color)
output_frame = tkinter.Frame(monitors, bg=menu_color)
button_frame = tkinter.Frame(monitors, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input monitors layout
list_entry3 = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button3 = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name3)
output_frame.pack()
list_entry3.grid(row=0, column=0)
list_add_button3.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output monitors layout
my_scrollbar3 = tkinter.Scrollbar(output_frame)
my_listbox3 = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar3.set)
# Link scrollbar to listbox
my_scrollbar3.config(command=my_listbox2.yview)
my_listbox3.grid(row=0, column=0)
my_scrollbar3.grid(row=0, column=1, sticky=NS)
# Button monitors Layout
remove_item_button3 = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item3)
save_button3 = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list3)
quit_button3 = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button3.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button3.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button3.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Define webcams Layout
input_frame = tkinter.Frame(webcams, bg=menu_color)
output_frame = tkinter.Frame(webcams, bg=menu_color)
button_frame = tkinter.Frame(webcams, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input webcams layout
list_entry4 = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button4 = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name4)
output_frame.pack()
list_entry4.grid(row=0, column=0)
list_add_button4.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output webcams layout
my_scrollbar4 = tkinter.Scrollbar(output_frame)
my_listbox4 = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar4.set)
# Link scrollbar to listbox
my_scrollbar4.config(command=my_listbox1.yview)
my_listbox4.grid(row=0, column=0)
my_scrollbar4.grid(row=0, column=1, sticky=NS)
# Button webcams Layout
remove_item_button1 = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item4)
save_button4 = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list4)
quit_button4 = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button1.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button4.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button4.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Define headsets Layout
input_frame = tkinter.Frame(headsets, bg=menu_color)
output_frame = tkinter.Frame(headsets, bg=menu_color)
button_frame = tkinter.Frame(headsets, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input headsets layout
list_entry5 = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button5 = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name5)
output_frame.pack()
list_entry5.grid(row=0, column=0)
list_add_button5.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output headsets layout
my_scrollbar5 = tkinter.Scrollbar(output_frame)
my_listbox5 = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar5.set)
# Link scrollbar to listbox
my_scrollbar5.config(command=my_listbox5.yview)
my_listbox5.grid(row=0, column=0)
my_scrollbar5.grid(row=0, column=1, sticky=NS)
# Button headsets Layout
remove_item_button5 = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item5)
save_button5 = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list5)
quit_button5 = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button5.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button5.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button5.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Define Pucks Layout
input_frame = tkinter.Frame(pucks, bg=menu_color)
output_frame = tkinter.Frame(pucks, bg=menu_color)
button_frame = tkinter.Frame(pucks, bg=button_color)
input_frame.pack()
button_frame.pack()
# Input Docks layout
list_entry6 = tkinter.Entry(input_frame, width=41, borderwidth=5, font=my_font)
list_add_button6 = tkinter.Button(input_frame, text="Add A Name", borderwidth=5, font=my_font, bg=button_color, command=add_name6)
output_frame.pack()
list_entry6.grid(row=0, column=0)
list_add_button6.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output Docks layout
my_scrollbar6 = tkinter.Scrollbar(output_frame)
my_listbox6 = tkinter.Listbox(output_frame, height=15, width=60, borderwidth=3, font=my_font, yscrollcommand=my_scrollbar6.set)
# Link scrollbar to listbox
my_scrollbar6.config(command=my_listbox1.yview)
my_listbox6.grid(row=0, column=0)
my_scrollbar6.grid(row=0, column=1, sticky=NS)
# Button Docks Layout
remove_item_button6 = tkinter.Button(button_frame, text="Remove Name", borderwidth=5, font=my_font, bg=button_color, command=remove_item6)
save_button6 = tkinter.Button(button_frame, text="Save", borderwidth=5, font=my_font, bg=button_color, command=save_list6)
quit_button6 = tkinter.Button(button_frame, text="Quit", borderwidth=5, font=my_font, bg=button_color, command=root.destroy)
remove_item_button6.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
save_button6.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
quit_button6.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
# Open the previous list if available
open_list1()
open_list2()
open_list3()
open_list4()
open_list5()
open_list6()
# Run the root windows's main loop
root.mainloop()
你可以定义一个类来嵌入这些东西然后你可以使用这个类来创建这些框架:
import tkinter as tk
from tkinter import ttk
class TabFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
self.filepath = kwargs.pop('filepath', None)
super().__init__(master, **kwargs)
self.input_frame = tk.Frame(self, bg=menu_color)
self.output_frame = tk.Frame(self, bg=menu_color)
self.button_frame = tk.Frame(self, bg=button_color)
self.input_frame.pack()
self.button_frame.pack()
self.output_frame.pack()
# Input layout
self.list_entry = tk.Entry(self.input_frame, width=41, borderwidth=5, font=my_font)
self.list_add_button = tk.Button(self.input_frame, text="Add A Name", borderwidth=5,
font=my_font, bg=button_color, command=self.add_name)
self.list_entry.grid(row=0, column=0)
self.list_add_button.grid(row=0, column=1, padx=2, pady=5, ipadx=5)
# Output layout
self.my_scrollbar = tk.Scrollbar(self.output_frame)
self.my_listbox = tk.Listbox(self.output_frame, height=15, width=60, borderwidth=3,
font=my_font, yscrollcommand=self.my_scrollbar.set)
self.my_scrollbar.config(command=self.my_listbox.yview)
self.my_listbox.grid(row=0, column=0)
self.my_scrollbar.grid(row=0, column=1, sticky=tk.NS)
# Button layout
self.remove_item_button = tk.Button(self.button_frame, text="Remove Name", borderwidth=5,
font=my_font, bg=button_color, command=self.remove_item)
self.save_button = tk.Button(self.button_frame, text="Save", borderwidth=5, font=my_font,
bg=button_color, command=self.save_list)
self.quit_button = tk.Button(self.button_frame, text="Quit", borderwidth=5, font=my_font,
bg=button_color, command=root.destroy)
self.remove_item_button.grid(row=0, column=1, padx=5, pady=5, ipadx=5)
self.save_button.grid(row=0, column=2, padx=5, pady=5, ipadx=30)
self.quit_button.grid(row=0, column=3, padx=5, pady=5, ipadx=30)
self.open_list()
# Define functions
def add_name(self):
"""Add name to list box"""
self.my_listbox.insert(tk.END, self.list_entry.get())
self.list_entry.delete(0, tk.END)
def remove_item(self):
"""Remove the selected (ANCHOR) item from the listbox"""
self.my_listbox.delete(tk.ANCHOR)
def open_list(self):
if self.filepath:
"""Open list if there is one upon starting"""
try:
with open(self.filepath, 'r') as f:
for line in f:
self.my_listbox.insert(tk.END, line)
except Exception as ex:
print(ex)
def save_list(self):
if self.filepath:
"""Save file to Text File"""
with open(self.filepath, 'w') as f:
# listbox.get() returns a tuple
list_tuple = self.my_listbox.get(0, tk.END)
for item in list_tuple:
# Take proper precautions to include only on n for formatting purposes
if item.endswith("n"):
f.write(item)
else:
f.write(item + "n")
# Define fonts and colors
text_color = "#fffacd"
menu_color = "#6c769b"
root_color = "#6c899a"
button_color = "#6c898a"
my_font = ("Times New Roman", 10)
# Define Window
root = tk.Tk()
root.title("IT2Go NotePad")
#root.iconbitmap("pad.ico")
root.geometry('500x500')
root.config(bg=root_color)
# root.resizable(0, 0)
it2goNotebook = ttk.Notebook(root)
it2goNotebook.pack(pady=15, fill='both', expand=1)
# Create frame
docks = TabFrame(it2goNotebook, width=500, height=500, bg=root_color, filepath="docklist.txt")
kbcombo = TabFrame(it2goNotebook, width=500, height=500, bg=menu_color)
monitors = TabFrame(it2goNotebook, width=500, height=500, bg=root_color)
webcams = TabFrame(it2goNotebook, width=500, height=500, bg=menu_color)
headsets = TabFrame(it2goNotebook, width=500, height=500, bg=root_color)
pucks = TabFrame(it2goNotebook, width=500, height=500, bg=menu_color)
it2goNotebook.add(docks, text="Docks")
it2goNotebook.add(kbcombo, text="KB Combo")
it2goNotebook.add(monitors, text="Monitors")
it2goNotebook.add(webcams, text="Webcams")
it2goNotebook.add(headsets, text="Headsets")
it2goNotebook.add(pucks, text="Pucks")
# Run the root windows's main loop
root.mainloop()