tkinter 文本 window 中的文本靠近滚动条
text in tkinter Text window is close to scrollbar
我制作了一个添加了滚动条的文本编辑器。
我这里有两个主要问题。
我面临的问题是我的文本在下一行时没有完全转到下一行
靠近右侧,最后一个字符恰好隐藏在滚动条下方。我想要
单词应该移动到下一行或者字符不应该在滚动条后面。
我有一个更改字体的选项,但字体列表未排序,很难找到特定的字体。我不知道如何对其进行排序或更好地选择如何添加搜索小部件以选择特定字体,就像在 Microsoft Word 编辑器中一样。
这是我的代码:
import os.path
from tkinter import *
from tkinter import colorchooser, font
from tkinter.messagebox import *
from tkinter.filedialog import *
'''
This is a text editior which can open save make new files
The fonts and color of text is changeable.
'''
def change_color():
color = colorchooser.askcolor(title="Pick any color")
text_area.config(fg=color[1])
def change_font(*args):
text_area.config(font=(font_name.get(), size_box.get()))
def new_file():
window.title("Untitled")
text_area.delete(1.0, END)
def open_file():
# from filedialog module it gives just the file directry and opening GUI
file_name = askopenfilename(defaultextension=".txt",
filetypes=[("Text Documents", '.txt'),
("All files",'*.*')])
# if window is closed without selecting a file.
# comparing it to none did not have effect as file is str type
# and has empty string when nothing is selected
if file_name == "":
print("No file was selected")
return
try:
window.title(os.path.basename(file_name))
text_area.delete(1.0, END)
# This builtin open function opens the file in given address
file = open(file_name, 'r')
# writes at start the strings text in file.read()
text_area.insert(1.0, file.read())
except Exception:
print("File could not be opened")
finally:
file.close()
def save_file():
file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension='.txt',
filetypes=[('All files','.*'),
('Text Documents','.txt')])
# if window is closed without selecting a file.
# comparing it to none did not have effect as file is str type
# and has empty string when nothing is selected
if file == "" :
print("No file was selected")
return
try:
window.title(os.path.basename(file))
file = open(file, 'w')
file.write(text_area.get(1.0, END))
except Exception:
print("Could not save file")
finally:
file.close()
def copy():
text_area.event_generate("<<Copy>>") # dont know how this function works
def cut():
text_area.event_generate("<<Cut>>") # dont know how this function works
def paste():
text_area.event_generate("<<Paste>>") # dont know how this function works
def about():
# Using message box modue
howinfo("About this program","This is a text editor")
def close():
window.destroy()
window = Tk()
window.title("Text Editor")
window_height = 500
window_width = 500
# Screen display size of my PC
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
print(screen_width)
print(screen_height)
# To make the window appear in centre.
x = int((screen_width / 2) - (window_width / 2))
y = int((screen_height / 2) - (window_height / 2))
window.geometry('{}x{}+{}+{}'.format(window_width, window_height,x,y))
# default font selection
font_name = StringVar(window)
font_name.set('Arial')
font_size = StringVar(window)
font_size.set("25")
# adding text area to write and attach scroll bar to it.
text_area = Text(window, font=(font_name.get(), font_size.get()))
scrollbar = Scrollbar(text_area)
scrollbar.pack(side=RIGHT, fill=Y)
# adding scroll bar to our text area
text_area.config(yscrollcommand=scrollbar.set)
#This line is making text move by scroll.
scrollbar.config(command=text_area.yview, cursor='arrow')
# This will make text to span when window is expanded or compressed
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
text_area.grid(sticky=N + E + S + W)
frame =Frame(window)
frame.grid()
# making color change , spin and font changing buttons
color_button = Button(frame, text="color", command=change_color)
color_button.grid(row=0,column=0)
# Option menu from font class
font_change = OptionMenu(frame, font_name, *font.families(),
command=change_font)
font_change.grid(row=0, column=1)
#spinbox for changing font size
size_box = Spinbox(frame, from_=1, to=100, textvariable=font_size,
command=change_font)
size_box.grid(row=0, column=2)
#menu tabs
menu_bar = Menu(window)
window.config(menu=menu_bar)
# File menu
file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New File", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=close)
# Edit menu
edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="copy", command=copy)
edit_menu.add_command(label="paste", command=paste)
edit_menu.add_command(label="cut", command=cut)
edit_menu.add_separator()
# help menu
help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='help', menu=help_menu)
help_menu.add_command(label='About', command=about)
window.mainloop()
问题图片:
对于问题 1,您需要将滚动条放在 window
而不是 text_area
:
text_area = Text(window, font=(font_name.get(), font_size.get()))
scrollbar = Scrollbar(window)
...
text_area.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.grid(row=0, column=1, sticky=N+S)
对于问题 2,您可以使用 sorted()
:
对列表进行排序
font_change = OptionMenu(frame, font_name, *sorted(font.families()),
command=change_font)
#TK
text_area0 = 文本(window, 字体=(font_name.get(), font_size.get()))
滚动条1 = 滚动条(window)
text_area.grid(行=0,列=0,粘性=N+E+S+W)
scrollbar.grid(行=0,列=1,粘性=N+S)
我制作了一个添加了滚动条的文本编辑器。 我这里有两个主要问题。
我面临的问题是我的文本在下一行时没有完全转到下一行 靠近右侧,最后一个字符恰好隐藏在滚动条下方。我想要 单词应该移动到下一行或者字符不应该在滚动条后面。
我有一个更改字体的选项,但字体列表未排序,很难找到特定的字体。我不知道如何对其进行排序或更好地选择如何添加搜索小部件以选择特定字体,就像在 Microsoft Word 编辑器中一样。
这是我的代码:
import os.path
from tkinter import *
from tkinter import colorchooser, font
from tkinter.messagebox import *
from tkinter.filedialog import *
'''
This is a text editior which can open save make new files
The fonts and color of text is changeable.
'''
def change_color():
color = colorchooser.askcolor(title="Pick any color")
text_area.config(fg=color[1])
def change_font(*args):
text_area.config(font=(font_name.get(), size_box.get()))
def new_file():
window.title("Untitled")
text_area.delete(1.0, END)
def open_file():
# from filedialog module it gives just the file directry and opening GUI
file_name = askopenfilename(defaultextension=".txt",
filetypes=[("Text Documents", '.txt'),
("All files",'*.*')])
# if window is closed without selecting a file.
# comparing it to none did not have effect as file is str type
# and has empty string when nothing is selected
if file_name == "":
print("No file was selected")
return
try:
window.title(os.path.basename(file_name))
text_area.delete(1.0, END)
# This builtin open function opens the file in given address
file = open(file_name, 'r')
# writes at start the strings text in file.read()
text_area.insert(1.0, file.read())
except Exception:
print("File could not be opened")
finally:
file.close()
def save_file():
file = asksaveasfilename(initialfile='Untitled.txt',
defaultextension='.txt',
filetypes=[('All files','.*'),
('Text Documents','.txt')])
# if window is closed without selecting a file.
# comparing it to none did not have effect as file is str type
# and has empty string when nothing is selected
if file == "" :
print("No file was selected")
return
try:
window.title(os.path.basename(file))
file = open(file, 'w')
file.write(text_area.get(1.0, END))
except Exception:
print("Could not save file")
finally:
file.close()
def copy():
text_area.event_generate("<<Copy>>") # dont know how this function works
def cut():
text_area.event_generate("<<Cut>>") # dont know how this function works
def paste():
text_area.event_generate("<<Paste>>") # dont know how this function works
def about():
# Using message box modue
howinfo("About this program","This is a text editor")
def close():
window.destroy()
window = Tk()
window.title("Text Editor")
window_height = 500
window_width = 500
# Screen display size of my PC
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
print(screen_width)
print(screen_height)
# To make the window appear in centre.
x = int((screen_width / 2) - (window_width / 2))
y = int((screen_height / 2) - (window_height / 2))
window.geometry('{}x{}+{}+{}'.format(window_width, window_height,x,y))
# default font selection
font_name = StringVar(window)
font_name.set('Arial')
font_size = StringVar(window)
font_size.set("25")
# adding text area to write and attach scroll bar to it.
text_area = Text(window, font=(font_name.get(), font_size.get()))
scrollbar = Scrollbar(text_area)
scrollbar.pack(side=RIGHT, fill=Y)
# adding scroll bar to our text area
text_area.config(yscrollcommand=scrollbar.set)
#This line is making text move by scroll.
scrollbar.config(command=text_area.yview, cursor='arrow')
# This will make text to span when window is expanded or compressed
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
text_area.grid(sticky=N + E + S + W)
frame =Frame(window)
frame.grid()
# making color change , spin and font changing buttons
color_button = Button(frame, text="color", command=change_color)
color_button.grid(row=0,column=0)
# Option menu from font class
font_change = OptionMenu(frame, font_name, *font.families(),
command=change_font)
font_change.grid(row=0, column=1)
#spinbox for changing font size
size_box = Spinbox(frame, from_=1, to=100, textvariable=font_size,
command=change_font)
size_box.grid(row=0, column=2)
#menu tabs
menu_bar = Menu(window)
window.config(menu=menu_bar)
# File menu
file_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New File", command=new_file)
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=close)
# Edit menu
edit_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="copy", command=copy)
edit_menu.add_command(label="paste", command=paste)
edit_menu.add_command(label="cut", command=cut)
edit_menu.add_separator()
# help menu
help_menu = Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(label='help', menu=help_menu)
help_menu.add_command(label='About', command=about)
window.mainloop()
问题图片:
对于问题 1,您需要将滚动条放在 window
而不是 text_area
:
text_area = Text(window, font=(font_name.get(), font_size.get()))
scrollbar = Scrollbar(window)
...
text_area.grid(row=0, column=0, sticky=N+E+S+W)
scrollbar.grid(row=0, column=1, sticky=N+S)
对于问题 2,您可以使用 sorted()
:
font_change = OptionMenu(frame, font_name, *sorted(font.families()),
command=change_font)
#TK
text_area0 = 文本(window, 字体=(font_name.get(), font_size.get()))
滚动条1 = 滚动条(window)
text_area.grid(行=0,列=0,粘性=N+E+S+W)
scrollbar.grid(行=0,列=1,粘性=N+S)