Python tkinter 滚动条小部件未正确显示
Python tkinter scrollbar widget not being displayed properly
我正在使用 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()
试试这个:
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
# Create the window
window = tk.Tk()
window.title("Text Editor")
window.state("zoomed") # This is the proper way of making the app fullscreen
# The frame, text box and scrollbar
frame = tk.Frame(window, bd=2, relief="raised")
text_box = tk.Text(window)
scroll_bar = tk.Scrollbar(window, command=text_box.yview)
frame.pack(side="left", fill="y")
text_box.pack(side="left", fill="both", expand=True)
scroll_bar.pack(side="left", fill="y")
# Connect the text box and the scrollbar
text_box.configure(yscrollcommand=scroll_bar.set)
# The buttons
button_kwargs = dict(width=10, height=2, bg="white")
open_b = tk.Button(frame, text="Open", **button_kwargs)
save_b = tk.Button(frame, text="Save As", **button_kwargs)
font_info = tk.Button(frame, text="Font", **button_kwargs)
# The 100 is the distance from the top edge of the frame
# The 5 is the distance between the buttons in the y direction
# The 50 is the distance from both walls of the frame in the x direction
open_b.pack(padx=50, pady=(100, 5), anchor="n")
save_b.pack(padx=50, pady=5)
font_info.pack(padx=50, pady=5)
window.mainloop()
布局看起来就像您拥有的一样。我将所有 .place
换成了 .pack
,因为我发现使用 .pack
更容易。此外,当使用 .pack
时,您可以让小部件决定自己的大小。我还使用 window.state("zoomed")
使应用全屏显示。有关 .pack
几何管理器如何工作的更多信息,请查看 here.
请注意,我删除了功能和按钮的 command
,因为这个问题是关于将小部件放在正确的位置。
我正在使用 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()
试试这个:
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
# Create the window
window = tk.Tk()
window.title("Text Editor")
window.state("zoomed") # This is the proper way of making the app fullscreen
# The frame, text box and scrollbar
frame = tk.Frame(window, bd=2, relief="raised")
text_box = tk.Text(window)
scroll_bar = tk.Scrollbar(window, command=text_box.yview)
frame.pack(side="left", fill="y")
text_box.pack(side="left", fill="both", expand=True)
scroll_bar.pack(side="left", fill="y")
# Connect the text box and the scrollbar
text_box.configure(yscrollcommand=scroll_bar.set)
# The buttons
button_kwargs = dict(width=10, height=2, bg="white")
open_b = tk.Button(frame, text="Open", **button_kwargs)
save_b = tk.Button(frame, text="Save As", **button_kwargs)
font_info = tk.Button(frame, text="Font", **button_kwargs)
# The 100 is the distance from the top edge of the frame
# The 5 is the distance between the buttons in the y direction
# The 50 is the distance from both walls of the frame in the x direction
open_b.pack(padx=50, pady=(100, 5), anchor="n")
save_b.pack(padx=50, pady=5)
font_info.pack(padx=50, pady=5)
window.mainloop()
布局看起来就像您拥有的一样。我将所有 .place
换成了 .pack
,因为我发现使用 .pack
更容易。此外,当使用 .pack
时,您可以让小部件决定自己的大小。我还使用 window.state("zoomed")
使应用全屏显示。有关 .pack
几何管理器如何工作的更多信息,请查看 here.
请注意,我删除了功能和按钮的 command
,因为这个问题是关于将小部件放在正确的位置。