【发布时间】:2021-10-15 07:43:59
【问题描述】:
我正在使用Python tkinter 开发一个文本编辑器应用程序。我正在尝试在我的程序中实现一个垂直滚动条,该滚动条将用于在文件中向下滚动。问题是滚动条小部件显示在屏幕的最右侧,几乎看不到,使其无法使用。请帮忙。我的代码:
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
window = tk.Tk()
window.title("Text Editor")
window.configure(bg="white")
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry("%dx%d" % (width, height))
def open_file():
file_path = askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if not file_path:
return
text_box.delete("1.0", tk.END)
with open(file_path, "r") as file_read:
text = file_read.read()
text_box.insert(tk.END, text)
window.title(f"Text Editor - {file_path}")
def save_file():
file_path = asksaveasfilename(defaultextension="txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if not file_path:
return
with open(file_path, "w") as file_write:
text = text_box.get(1.0, tk.END)
file_write.write(text)
window.title(f"Text Editor - {file_path}")
def font_command():
window2 = tk.Toplevel(window)
window2.title("Font Name")
window2.geometry("300x150+500+200")
text = text_box["font"]
font_n = tk.Label(window2, text="Font:", font=("Arial Rounded MT Bold", 10))
font_n.place(x=50, y=80)
name_entry = tk.Entry(window2)
name_entry.place(x=50, y=110)
name_entry.delete(0, tk.END)
name_entry.insert(tk.END, text)
ok_button = tk.Button(window2, text="OK", width=8, height=2, bg="white")
ok_button.place(x=200, y=70)
font_size = tk.Label(window2, text="Font Size:", font=("Arial Rounded MT Bold", 10))
font_size.place(x=50, y=20)
size_entry = tk.Entry(window2)
size_entry.place(x=50, y=50)
size_entry.delete(0, tk.END)
size_entry.insert(tk.END, text)
frame = tk.Frame(window, width=250, height=height, bd=2, relief=tk.RAISED)
frame.place(x=0, y=0)
open_b = tk.Button(frame, text="Open", width=10, height=2, bg="white", command=open_file)
open_b.place(x=70, y=20)
save_b = tk.Button(frame, text="Save As", width=10, height=2, bg="white", command=save_file)
save_b.place(x=70, y=60)
new_width = width - 300
text_box = tk.Text(window, width=new_width, height=height)
text_box.place(x=250, y=0)
scroll_bar = tk.Scrollbar(window, command=text_box.yview)
scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
text_box.configure(yscrollcommand=scroll_bar.set)
font_info = tk.Button(frame, text="Font", width=10, bg="white", height=2,
command=font_command)
font_info.place(x=70, y=100)
window.mainloop()
非常感谢 TheLizzard 的帮助!更新代码:
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
window = tk.Tk()
window.title("Text Editor")
window.configure(bg="white")
window.state("zoomed")
def open_file():
file_path = askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if not file_path:
return
text_box.delete("1.0", tk.END)
with open(file_path, "r") as file_read:
text = file_read.read()
text_box.insert(tk.END, text)
window.title(f"Text Editor - {file_path}")
def save_file():
file_path = asksaveasfilename(defaultextension="txt", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if not file_path:
return
with open(file_path, "w") as file_write:
text = text_box.get(1.0, tk.END)
file_write.write(text)
window.title(f"Text Editor - {file_path}")
def font_command():
window2 = tk.Toplevel(window)
window2.title("Font Name")
window2.geometry("300x150+500+200")
text = text_box["font"]
font_n = tk.Label(window2, text="Font:", font=("Arial Rounded MT Bold", 10))
font_n.place(x=50, y=80)
name_entry = tk.Entry(window2)
name_entry.place(x=50, y=110)
name_entry.delete(0, tk.END)
name_entry.insert(tk.END, text)
ok_button = tk.Button(window2, text="OK", width=8, height=2, bg="white")
ok_button.place(x=200, y=70)
font_size = tk.Label(window2, text="Font Size:", font=("Arial Rounded MT Bold", 10))
font_size.place(x=50, y=20)
size_entry = tk.Entry(window2)
size_entry.place(x=50, y=50)
size_entry.delete(0, tk.END)
size_entry.insert(tk.END, 20)
frame = tk.Frame(window, bd=2, relief="raised")
frame.pack(side="left", fill="y")
text_box = tk.Text(window)
text_box.pack(side="left", fill="both", expand=True)
scroll_bar = tk.Scrollbar(window, command=text_box.yview)
scroll_bar.pack(side="left", fill="y")
open_b = tk.Button(frame, text="Open", command=open_file, width=6, height=2)
open_b.pack(padx=50, pady=(100, 5), anchor="n")
save_b = tk.Button(frame, text="Save As", command=save_file, width=6, height=2)
save_b.pack(padx=50, pady=5)
font_info = tk.Button(frame, text="Font", command=font_command, width=6, height=2)
font_info.pack(padx=50, pady=5)
text_box.configure(yscrollcommand=scroll_bar.set)
window.mainloop()
【问题讨论】:
-
你想把滚动条放在哪里?
-
在屏幕右侧
-
您指定了文本框的宽度,但它应该是字符而不是像素。
-
那如何以像素为单位指定呢?
-
你不能。您可以使用
.pack管理器并使用fill="both"让文本框选择自己的大小。我正在写一个答案