我制作了一個添加了滾動條的文本編輯器。我這里有兩個主要問題。
我面臨的問題是我的文本在靠近右側時并沒有完全轉到下一行,最后一個字符只是隱藏在滾動條下。我希望這個詞要么移到下一行,要么字符不應該放在滾動條后面。
我可以選擇更改字體,但字體串列未排序,很難找到特定的字體。我不知道如何對其進行排序或更好的選擇如何添加搜索小部件以選擇特定字體,就像在 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()
問題圖片:


uj5u.com熱心網友回復:
對于問題 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446475.html
標籤:Python tkinter 字体 滚动条 tkinter.optionmenu
上一篇:tkinter中奇怪的滾動條UI
