我正在尝试使用Tkinter的网格管理器获取窗口的特定布局。所需的布局是:
_______________________________________________________
| | | |
| | | |
| canvas A | canvas B | canvas C |
| | | |
| | | |
|------------------------------------------------------
| B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7 | B8 |
| ____|_____|_____|_____|_____|_____|_____|_____|_____|
其中B代表9个按钮之一。下面的代码很接近,但在我看来,画布A和C只是忽略columnspan=3
参数而画布B正确使用它。
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import Tkinter as Tk
def generate_window():
root = Tk.Tk()
root.rowconfigure((0,1), weight=1, minsize=35)
root.columnconfigure((0,8), weight=1, minsize=200)
titles = ['A', 'B', 'C']
for t, n in zip(titles, xrange(1, 4)):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3,4], [1,4,9,16], 'ro')
ax.set_title(t)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0, column=(n-1)*3, columnspan=3, sticky='NSEW')
for x in xrange(0, 9):
Tk.button = Tk.Button(master=root, text=str(x), command= lambda x=x:button(x))
Tk.button.grid(row=1, column=x, sticky='NSEW')
Tk.mainloop()
def button(button_number):
print button_number
generate_window()
This类似的措辞问题并没有解决这个问题,因为这里没有空列
答案 0 :(得分:2)
您只是将列权重应用于几列,因此您最终会得到不相等的列宽。如果您希望所有列的大小相同,最简单的解决方案是为它们提供相同的权重和/或使用uniform
选项。
例如:
root.columnconfigure((0,1,2,3,4,5,6,7,8), weight=1)