参考Tkinter Pack() setting a ratio for expanding
在pack方法构建的PySimpleGUI下无法实现。
这是在 PySimpleGUI 下将几何管理器从 pack 更改为 grid 的黑客方法,但可见/不可见选项将不再起作用。
import PySimpleGUI as sg
headings = ['President', 'Date of Birth']
data = [
['Ronald Reagan', 'February 6'],
['Abraham Lincoln', 'February 12'],
['George Washington', 'February 22'],
['Andrew Jackson', 'March 15'],
['Thomas Jefferson', 'April 13'],
['Harry Truman', 'May 8'],
['John F. Kennedy', 'May 29'],
['George H. W. Bush', 'June 12'],
['George W. Bush', 'July 6'],
['John Quincy Adams', 'July 11'],
['Garrett Walker', 'July 18'],
['Bill Clinton', 'August 19'],
['Jimmy Carter', 'October 1'],
['John Adams', 'October 30'],
['Theodore Roosevelt', 'October 27'],
['Frank Underwood', 'November 5'],
['Woodrow Wilson', 'December 28'],
]
size = (480, 320)
font = ('Courier New', 11)
sg.theme('DarkBlue3')
sg.set_options(font=font)
layout = [
[sg.Table(data, headings=headings, justification='left', expand_x=True, expand_y=True, key='-TABLE-')],
[sg.Graph(size, (0, 0), size, background_color='green', expand_x=True, expand_y=True, key='-GRAPH-')],
]
window = sg.Window('Title', layout, resizable=True, finalize=True)
height = window['-TABLE-'].Widget.winfo_reqheight()//2
window['-GRAPH-'].Widget.configure(height=height)
width = window['-GRAPH-'].CanvasSize[0]
window['-GRAPH-'].CanvasSize = (width, height)
table = window['-TABLE-'].Widget.master.master
table.pack_forget()
graph = window['-GRAPH-'].Widget.master
graph.pack_forget()
table.grid(row=0, column=0, sticky=sg.tk.NSEW)
graph.grid(row=1, column=0, sticky=sg.tk.NSEW)
root = table.master
root.rowconfigure(0, weight=2)
root.rowconfigure(1, weight=1)
root.columnconfigure(0, weight=1)
# Size of Graph changed, maybe you need to update following arguments
# window['-GRAPH-'].change_coordinates(graph_bottom_left, graph_top_right)
window.read(close=True)
如果窗口不能调整大小,会容易得多,只需使用语句window = sg.Window(...)中的以下代码
window = sg.Window('Title', layout, finalize=True)
height = window['-TABLE-'].Widget.winfo_reqheight()//2
window['-GRAPH-'].Widget.configure(height=height)
width = window['-GRAPH-'].CanvasSize[0]
window['-GRAPH-'].CanvasSize = (width, height)
# Size of Graph changed, maybe you need to update following arguments
# window['-GRAPH-'].change_coordinates(graph_bottom_left, graph_top_right)
window.read(close=True)