首页 > 解决方案 > Python tkinter 滚动条小部件未正确显示

问题描述

我正在使用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()

标签: pythontkinterscrollbar

解决方案


尝试这个:

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()

布局看起来就像您拥有的一样。我把所有的.places都换成了 s,.pack因为我发现使用 s 更容易.pack。此外,在使用时,.pack您可以让小部件决定它们自己的大小。我还window.state("zoomed")用来使应用程序全屏显示。有关.pack几何管理器如何工作的更多信息,请查看此处

请注意,我删除了功能和按钮command,因为这个问题是关于将小部件放在正确的位置。


推荐阅读