Tkinter TreeVIew 水平滚动条不起作用

Tkinter TreeVIew Horizontal Scrollbar not working

提问人:newbies 提问时间:11/5/2023 更新时间:11/6/2023 访问量:45

问:

我正在使用 tkinter 和 treeview 构建一个普通表。水平滚动条是可见的,但未激活,尽管我已经配置了设置。想知道代码有什么问题吗?enter image description here

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter.ttk import LabelFrame
from PIL import ImageTk, Image
from tkcalendar import Calendar
from tkinter import messagebox
import webbrowser
import datetime
from io import BytesIO
from tkinter import filedialog


login_window = tk.Tk()
login_window.geometry('950x600')

Table_frame = tk.Frame(login_window, bd=15, relief='ridge', width=900, height=200)
Table_frame.grid(row=3, column=0, sticky='nsew')

destination_table = ttk.Treeview(Table_frame)
destination_table.pack(fill=BOTH, expand=1)

scroll_x = ttk.Scrollbar(Table_frame, orient='horizontal')
scroll_x.configure(command=destination_table.xview)
scroll_x.pack(side=BOTTOM, fill=X)

destination_table.configure(xscrollcommand = scroll_x.set)

destination_table["columns"]=("destinationname", "rate", "description", "estimatedprice","address", "contact", "operationtime", "type", "tags")

destination_table["show"] = "headings"

destination_table.heading("destinationname", text="Destination Name")
destination_table.heading("rate", text="Ratings")
destination_table.heading("description", text="Description")
destination_table.heading("estimatedprice", text="Estimated Price")
destination_table.heading("address", text="Address")
destination_table.heading("contact", text="Contact")
destination_table.heading("operationtime", text="Operation Time")
destination_table.heading("type", text="Destination type")
destination_table.heading("tags", text="Tags")


destination_table.column("destinationname", width=100)

login_window.mainloop()
python tkinter 树视图 滚动条

评论

0赞 Сергей Кох 11/5/2023
stackoverflow.com/questions/3085696/......
0赞 toyota Supra 11/5/2023
@newbies。您的代码正在工作。你有这个.您必须调整窗口大小。然后列“标签”调整到右侧,然后您将看到一个向左移动的水平条。geometry('950x600')

答:

0赞 acw1668 11/6/2023 #1

默认情况下,树视图将调整大小以显示所有列,因此不会激活水平滚动条。destination_table

您可以调整父框架的大小以适合其父窗口的宽度,然后其中的树视图也将调整大小以适合其宽度。然后水平滚动条将被激活,因为宽度不够宽,无法显示所有列。Table_frame

由于框架上使用网格布局管理器,只需添加以下行即可使框架适合窗口宽度:Table_frame

login_window.columnconfigure(0, weight=1)

结果:

enter image description here