Create a simple app with tabbed windows in Tkinter. Introduced a tabbed window to the "editor" style app created in the previous "Easy GUI app with Tkinter Text" to edit multiple files. Make it possible. Use the Notebook widget of the tkinter.ttk module to create a tabbed window.
It is an "editor" style application that uses a tabbed window.
Import tkinter and tkinter.ttk.
import tkinter as tk
import tkinter.ttk as ttk
Create a Notebook and place it in the app window.
root = tk.Tk()
notebook = ttk.Notebook(root)
notebook.pack(fill='both',expand=1)
Create a frame to place on the tab and add the tab with Notebook.add (). If you leave it as it is, the added tab is hidden behind it, so use Notebook.select () to bring it to the front. Notebook.tabs () returns a tab_id list and Notebook.index ('end') returns the number of tabs, so reduce the number of tabs by -1 and pass the last tab_id in the list to Notebook.select ().
frame=tk.Frame(notebook)
notebook.add(frame,text='title')
notebook.select(notebook.tabs()[notebook.index('end')-1])
Returns the tab_id of the tab selected by Notebook.select () with no arguments. You can check the number of tabs added by using the tab_id selected for the index () method of the tab_id list returned by Notebook.tabs () as an argument.
idx=notebook.tabs().index(notebook.select())
import os
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
class SbTextFrame(tk.Frame):
def __init__(self,master):
super().__init__(master)
text = tk.Text(self,wrap='none',undo=True)
x_sb = tk.Scrollbar(self,orient='horizontal')
y_sb = tk.Scrollbar(self,orient='vertical')
x_sb.config(command=text.xview)
y_sb.config(command=text.yview)
text.config(xscrollcommand=x_sb.set,yscrollcommand=y_sb.set)
text.grid(column=0,row=0,sticky='nsew')
x_sb.grid(column=0,row=1,sticky='ew')
y_sb.grid(column=1,row=0,sticky='ns')
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.text = text
self.x_sb = x_sb
self.y_sb = y_sb
def add_tab(fname):
global tframes,fnames,notebook
tframe=SbTextFrame(notebook)
tframes.append(tframe)
if os.path.isfile(fname):
f=open(fname,'r')
lines=f.readlines()
f.close()
for line in lines:
tframe.text.insert('end',line)
fnames.append(fname)
title=os.path.basename(fname)
notebook.add(tframe,text=title)
notebook.select(notebook.tabs()[notebook.index('end')-1])
def fileopen():
fname = filedialog.askopenfilename()
add_tab(fname)
def filesave():
global tframes,fnames,notebook
idx = notebook.tabs().index(notebook.select())
fname = fnames[idx]
tframe = tframes[idx]
f = open(fname,'w')
f.writelines(tframe.text.get('1.0','end-1c'))
f.close()
def main():
global root,notebook,tframes,fnames
root = tk.Tk()
root.title('tabbed editor')
root.geometry('400x300')
notebook = ttk.Notebook(root)
notebook.pack(fill='both',expand=1)
tframes = []
fnames = []
add_tab('new')
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar,tearoff=0)
filemenu.add_command(label='Open',command=fileopen)
filemenu.add_command(label='Save',command=filesave)
filemenu.add_command(label='Exit',command=exit)
menubar.add_cascade(label='File',menu=filemenu)
root.config(menu=menubar)
root.mainloop()
if __name__ == '__main__':
main()
This time, I made an "editor" style application that uses a tabbed window. When using multiple windows, not just the "editor", it's nice to use tabs to make the screen cleaner.
Recommended Posts