如何使Python的tkinter treeview部件填充网格上的空行和列空间?

huangapple go评论109阅读模式
英文:

How to make python tkinter treeview widget to fill empty row and column spaces on grid?

问题

以下是您提供的代码的翻译部分:

  1. 我有一个窗口来显示多个树状视图小部件当前当我展开它时树状视图下面有一些空白行空间我不确定如何在展开窗口时动态填充下面的空白空间我已经尝试调整 rowspan/columnspan 选项但无法获得如下图所示的期望输出我还在下面包含了一个可重现的代码用于测试我的程序请帮助建议我需要进行哪些更改谢谢

如果您需要更多帮助或有其他问题,请随时提出。

英文:

I have a window to display multiple treeview widgets. Currently there are some empty row spaces below the treeview when I expand it and I'm not sure on how to fill the empty spaces below dynamically when I expand the window. I have tried adjusting the rowspan/columnspan options but could not get the desired output as described in image below. I have also included a reproduceable code below for testing my program. Please help to advise on which part I need to make changes. Thanks!

如何使Python的tkinter treeview部件填充网格上的空行和列空间?

  1. import shutil
  2. import tkinter as tk
  3. import tkinter.ttk as ttk
  4. import tempfile
  5. import zipfile, re, os
  6. from tkinter import *
  7. from tkinter import filedialog
  8. from pathlib import Path
  9. import tkinter.messagebox
  10. from fpdf import FPDF
  11. from datetime import datetime, timedelta
  12. import ctypes
  13. DEPTH = 2
  14. EXCLUDES = {'__MACOSX', 'resources'}
  15. class HomePage(Tk):
  16. def __init__(self, *args, **kwargs):
  17. Tk.__init__(self, *args, **kwargs)
  18. self.notebook = ttk.Notebook() # Create a notebook widget
  19. self.add_tab1()
  20. self.notebook.grid(row=0)
  21. self.notebook.pack(expand=1, fill="both")
  22. def add_tab1(self):
  23. tab1 = tab_one(self.notebook)
  24. self.notebook.add(tab1, text="Home")
  25. class data_table(object):
  26. def __init__(self, site, panels_count, tev_count):
  27. self.site = site
  28. self.panels_count = panels_count
  29. self.tev_count = tev_count
  30. class tab_one(Frame):
  31. def __init__(self, *args, **kwargs):
  32. Frame.__init__(self, *args, **kwargs)
  33. # Creating frames on the window/canvas for the first tab
  34. frame_selectfile = tk.LabelFrame(self, text="Step 1: Zip File Extraction ", bd=6) # Frame1 on the window/canvas
  35. frame_selectfile.grid(column=0, row=0, padx=10, pady=10, sticky='NSEW') # Positioning frame1
  36. frame_selectfile.configure(borderwidth=1)
  37. self.grid_columnconfigure(1, weight=1) # Configuring the column for the main window/canvas
  38. self.grid_rowconfigure(1, weight=1) # Configuring the row for the main window/canvas
  39. frame_checkpd = tk.LabelFrame(self, text="Step 2: Check PD ", bd=6) # Frame2 on the window/canvas
  40. frame_checkpd.grid(column=1, row=0, padx=10, pady=10, sticky='NSEW') # Positioning frame2
  41. frame_checkpd.configure(borderwidth=1)
  42. self.grid_columnconfigure(1, weight=1) # Configuring the column for the main window/canvas
  43. self.grid_rowconfigure(1, weight=1) # Configuring the row for the main window/canvas
  44. #Frame to display data in treeview
  45. frame_tree = tk.LabelFrame(self, text="PD Results ", bd=6) # Frame2 on the window/canvas
  46. frame_tree.grid(column=0, row=1, columnspan=2, rowspan=2, padx=10, pady=10, sticky='NSEW') # Positioning frame2
  47. frame_tree.configure(borderwidth=1)
  48. self.grid_columnconfigure(1, weight=1) # Configuring the column for the main window/canvas
  49. self.grid_rowconfigure(1, weight=1) # Configuring the row for the main window/canvas
  50. # Initializing all variables in frame_selectfile
  51. file_ID = tk.StringVar()
  52. location_id = tk.StringVar()
  53. unzip_status = tk.StringVar()
  54. tmpdir = tempfile.TemporaryDirectory().name
  55. f_tem_sdir = open(os.getcwd()+"\\temp_dir.txt", "w")
  56. f_tem_sdir.write(tmpdir)
  57. f_tem_sdir.close()
  58. zip_filename = tk.StringVar()
  59. site_id = tk.StringVar()
  60. site_id.set("0")
  61. data_table_list = []
  62. middleframe = tk.LabelFrame(frame_selectfile, bd=5)
  63. databaseView = ttk.Treeview(middleframe, selectmode="browse")
  64. label_filename = tk.Label(frame_selectfile, text=" ", width=10, relief='flat').grid(column=0, row=1, padx=5,
  65. pady=5, sticky='NW')
  66. label_filelocation = tk.Label(frame_selectfile, text="File Location:", width=12, relief='flat').grid(column=0,
  67. row=2,
  68. padx=5,
  69. pady=5,
  70. sticky='NW')
  71. filename_Entry = tk.Label(frame_selectfile, width=52, textvariable=file_ID)
  72. filename_Entry.grid(column=1, row=1, padx=5, pady=5, sticky='NW')
  73. filename_Entry.configure(state='normal')
  74. filelocation_Entry = tk.Entry(frame_selectfile, width=63, textvariable=location_id)
  75. filelocation_Entry.grid(column=1, row=2, padx=5, pady=5, sticky='W')
  76. filelocation_Entry.configure(background='palegreen', foreground='black')
  77. unzip_status.set("")
  78. label_unzipstatus = tk.Label(frame_selectfile, width=20, relief='flat', textvariable=unzip_status)
  79. label_unzipstatus.grid(column=1, row=5, padx=5, pady=5, sticky='NSEW')
  80. selectzip_button = tk.Button(frame_selectfile, width=20, text="Select Zip File",
  81. command=lambda: self.upload_action(location_id, zip_filename, tmpdir))
  82. selectzip_button.grid(column=1, row=3, padx=5, pady=3, ipady=3, sticky='SW')
  83. unzip_button = tk.Button(frame_selectfile, width=20, text="Extract",
  84. command=lambda: self.extract_nested_zip(databaseView, data_table_list,
  85. location_id.get(), tmpdir, zip_filename,
  86. site_id, False))
  87. unzip_button.grid(column=1, row=3, padx=5, pady=3, ipady=3, sticky='NE')
  88. # Creating LabelFrame in frame_unzip
  89. upperframe = tk.LabelFrame(frame_selectfile, bd=0)
  90. frame_selectfile.rowconfigure(0, weight=1)
  91. frame_selectfile.columnconfigure(2, weight=1)
  92. upperframe.grid(column=0, row=6, columnspan=2, sticky='W')
  93. # middleframe = tk.LabelFrame(frame_selectfile, bd=5)
  94. middleframe.configure(borderwidth=1)
  95. frame_selectfile.columnconfigure(2, weight=1)
  96. frame_selectfile.rowconfigure(1, weight=1)
  97. middleframe.grid(column=0, row=7, columnspan=2, sticky="NSEW")
  98. lowerframe = tk.LabelFrame(frame_selectfile, bd=0)
  99. lowerframe.grid(column=0, row=7)
  100. frame_selectfile.columnconfigure(2, weight=1)
  101. frame_selectfile.rowconfigure(2, weight=0)
  102. # Labels in frame_unzip
  103. label18 = tk.Label(upperframe, text="Number of sites:", relief='flat')
  104. label18.grid(column=0, row=0, padx=5, pady=5, sticky='W')
  105. site_Entry = tk.Entry(upperframe, width=61, textvariable=site_id)
  106. site_Entry.grid(column=1, row=0, padx=10, pady=5, sticky='E')
  107. site_Entry.configure(state='normal', background='palegreen', foreground='black')
  108. label_details = tk.Label(upperframe, text=" ", relief='flat')
  109. label_details.grid(column=0, row=2, padx=5, pady=0, sticky='W')
  110. databaseView.columnconfigure(2, weight=1)
  111. databaseView.grid(column=0, row=0, columnspan=2, sticky="NSEW")
  112. # Creating treeview in frame_unzip
  113. vsb = Scrollbar(middleframe, orient="vertical", command=databaseView.yview())
  114. hsb = Scrollbar(middleframe, orient="horizontal")
  115. middleframe.columnconfigure(0, weight=1)
  116. middleframe.rowconfigure(0, weight=1)
  117. databaseView["show"] = "headings"
  118. databaseView["columns"] = ("site", "panels", "tevs")
  119. vsb.configure(command=databaseView.yview)
  120. vsb.grid(column=1, row=0, sticky="NS")
  121. # Treeview column headings
  122. databaseView.heading("site", text="Site")
  123. databaseView.column("site", anchor='w', width=250)
  124. databaseView.heading("panels", text="Number of Panels")
  125. databaseView.column("panels", anchor='center', width=150)
  126. databaseView.heading("tevs", text="Number of TEVs")
  127. databaseView.column("tevs", anchor='center', width=200)
  128. findpd_btn = tk.Button(frame_checkpd, width=20, text="Find PDs",
  129. command=lambda: self.run_pd_model_exe(tmpdir, zip_filename,parent_tree, tree))
  130. findpd_btn.grid(column=0, row=0, padx=5, pady=5, sticky='E')
  131. export_pdf_btn = tk.Button(frame_checkpd, width=20, text="Export to PDF",
  132. command=lambda: self.export_to_pdf(tmpdir, location_id.get()))
  133. export_pdf_btn.grid(column=1, row=0, padx=5, pady=5, sticky='E')
  134. find_files_btn = tk.Button(frame_checkpd, width=20, text="Clear All",
  135. command=lambda: self.clear_all_files(tmpdir,parent_tree,tree,databaseView))
  136. find_files_btn.grid(column=2, row=0, padx=5, pady=5, sticky='W')
  137. scrollbary = Scrollbar(frame_tree, orient=VERTICAL)
  138. scrollbarx = Scrollbar(frame_tree, orient=HORIZONTAL)
  139. parentframe = tk.LabelFrame(frame_checkpd, bd=0)
  140. frame_checkpd.rowconfigure(2, weight=1)
  141. frame_checkpd.columnconfigure(2, weight=1, )
  142. parentframe.grid(column=0, row=1, columnspan=3, sticky='W')
  143. # PARENT TREE
  144. parent_tree = ttk.Treeview(parentframe,
  145. columns=("1", "2",
  146. "3", "4",
  147. "5", "6",
  148. "7", "8"),
  149. selectmode="extended")
  150. parent_tree.grid(row=1, column=0, pady=2, sticky=N + S + E + W)
  151. #self.parent_tree.pack(expand=True, fill='both')
  152. vsbb = Scrollbar(parentframe, orient="vertical", command=parent_tree.yview())
  153. vsbb.configure(command=parent_tree.yview)
  154. vsbb.grid(column=2, row=1, sticky="NS")
  155. parent_tree.heading("#0", text="")
  156. parent_tree.heading("1", text="File ID")
  157. parent_tree.heading("2", text="Date")
  158. parent_tree.heading("3", text="No")
  159. parent_tree.heading("4", text="Engineer")
  160. parent_tree.heading("5", text="Station")
  161. parent_tree.heading("6", text="Voltage (kV)")
  162. parent_tree.heading("7", text="Max dB")
  163. parent_tree.heading("8", text="Max PD (%)")
  164. # parent_tree.heading("8", text = "Panel No")
  165. parent_tree.column('#0', stretch=YES, minwidth=0, width=5, anchor=CENTER)
  166. parent_tree.column('#1', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  167. parent_tree.column('#2', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  168. parent_tree.column('#3', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  169. parent_tree.column('#4', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  170. parent_tree.column('#5', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  171. parent_tree.column('#6', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  172. parent_tree.column('#7', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  173. parent_tree.column('#8', stretch=YES, minwidth=0, width=130, anchor=CENTER)
  174. # CHILD TREE
  175. tree = ttk.Treeview(frame_tree,
  176. columns=("1", "2", "3", "4"
  177. , "5", "6", "7", "8"
  178. , "9"),
  179. selectmode="extended")
  180. #yscrollcommand=scrollbary.set,
  181. #xscrollcommand=scrollbarx.set)
  182. tree.grid(row=0, column=0, pady=2, sticky=N + S + E + W)
  183. # scrollbary.config(command=tree.yview)
  184. # scrollbary.grid(row=0, column=1, pady=2, sticky=N + S)
  185. #
  186. # scrollbarx.config(command=tree.xview)
  187. # scrollbarx.grid(row=2, column=0, sticky=W + E)
  188. tree_vsb = Scrollbar(frame_tree, orient="vertical", command=tree.yview())
  189. tree_hsb = Scrollbar(frame_tree, orient="horizontal")
  190. tree_vsb.configure(command=tree.yview)
  191. tree_vsb.grid(column=1, row=0, sticky="NS")
  192. tree.heading("#0", text="")
  193. tree.heading("1", text="Panel No")
  194. tree.heading("2", text="TEV Name")
  195. tree.heading("3", text="Component")
  196. tree.heading("4", text="Sublocation")
  197. tree.heading("5", text="Phase Ref Lock")
  198. tree.heading("6", text="dB")
  199. tree.heading("7", text="PRPD")
  200. tree.heading("8", text="Pulse Wave")
  201. tree.heading("9", text="PD %")
  202. tree.column('#0', stretch=NO, minwidth=0, width=0, anchor=CENTER)
  203. tree.column('#1', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  204. tree.column('#2', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  205. tree.column('#3', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  206. tree.column('#4', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  207. tree.column('#5', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  208. tree.column('#6', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  209. tree.column('#7', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  210. tree.column('#8', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  211. tree.column('#9', stretch=YES, minwidth=0, width=175, anchor=CENTER)
  212. if __name__ == '__main__':
  213. homePage = HomePage()
  214. homePage.title("Waveform Identifier") # Window title
  215. screen_width = homePage.winfo_screenwidth()
  216. screen_height = homePage.winfo_screenheight()
  217. width = 1600
  218. height = 800
  219. x = (screen_width / 2) - (width / 2)
  220. y = (screen_height / 2) - (height / 2)
  221. homePage.geometry("%dx%d+%d+%d" % (width, height, x, y))
  222. homePage.resizable(1, 1)
  223. homePage.mainloop()

答案1

得分: 1

问题发现

  • frame_checkpd.rowconfigure(2, weight=1) 上的行号错误,应该是行 1。
  • parentframe.grid(column=0, row=1, columnspan=3, sticky='W') 上的粘性选项错误,应该是 'NSEW'。
  • 缺少 parentframe.rowconfigure(...)parentframe.columnconfigure(...)
  • 缺少 frame_tree.rowconfigure(...)frame_tree.columnconfigure(...)

以下是实现这些更改所需的代码:

  1. class tab_one(Frame):
  2. def __init__(self, *args, **kwargs):
  3. ...
  4. parentframe = tk.LabelFrame(frame_checkpd, bd=0)
  5. frame_checkpd.rowconfigure(1, weight=1) ### 将行 2 改为行 1
  6. frame_checkpd.columnconfigure(2, weight=1, )
  7. parentframe.grid(column=0, row=1, columnspan=3, sticky='NSEW') ### 将 'W' 改为 'NSEW'
  8. # 父级树
  9. parentframe.rowconfigure(1, weight=1) ### 添加
  10. parentframe.columnconfigure(0, weight=1) ### 添加
  11. ...
  12. # 子级树
  13. frame_tree.rowconfigure(0, weight=1) ### 添加
  14. frame_tree.columnconfigure(0, weight=1) ### 添加
  15. ...

结果:

如何使Python的tkinter treeview部件填充网格上的空行和列空间?

英文:

Issues found

  • Wrong row number on frame_checkpd.rowconfigure(2, weight=1), it should be row 1.
  • Wrong sticky option on parentframe.grid(column=0, row=1, columnspan=3, sticky='W'), it should be 'NSEW'.
  • missing parentframe.rowconfigure(...) and parentframe.columnconfigure(...)
  • missing frame_tree.rowconfigure(...) and frame_tree.columnconfigure(...)

Below is the required changes to achieve it:

  1. class tab_one(Frame):
  2. def __init__(self, *args, **kwargs):
  3. ...
  4. parentframe = tk.LabelFrame(frame_checkpd, bd=0)
  5. frame_checkpd.rowconfigure(1, weight=1) ### row 2 -> 1
  6. frame_checkpd.columnconfigure(2, weight=1, )
  7. parentframe.grid(column=0, row=1, columnspan=3, sticky='NSEW') ### 'W' -> 'EW'
  8. # PARENT TREE
  9. parentframe.rowconfigure(1, weight=1) ### added
  10. parentframe.columnconfigure(0, weight=1) ### added
  11. ...
  12. # CHILD TREE
  13. frame_tree.rowconfigure(0, weight=1) ### added
  14. frame_tree.columnconfigure(0, weight=1) ### added
  15. ...

Result:

如何使Python的tkinter treeview部件填充网格上的空行和列空间?

huangapple
  • 本文由 发表于 2023年1月6日 11:11:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定