Tkinter TreeView 水平滚动条不起作用

问题描述 投票:0回答:1

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

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 treeview scrollbar
1个回答
0
投票

树视图

destination_table
将调整大小以默认显示所有列,因此水平滚动条未激活。

您可以调整父框架

Table_frame
的大小以适应其父窗口的宽度,然后其内部的树视图也会调整大小以适应其宽度。然后水平滚动条将被激活,因为宽度不足以显示所有列。

由于grid布局管理器用于框架

Table_frame
,只需添加以下行即可使框架适合窗口宽度:

login_window.columnconfigure(0, weight=1)

结果:

© www.soinside.com 2019 - 2024. All rights reserved.