如何在不规则放置,不符合3x3网格的python tkinter中布置matplotlib条形图

时间:2018-09-19 09:19:02

标签: python matplotlib tkinter

以下python代码在两个滚动文本框和多个按钮后面显示一个条形图。我真的很想做 python ,我想知道是否可以调整图形的大小,以便:

(1)其左边缘与 滚动文本框 的右边缘对齐;

(2)其顶部与 按钮3 ;

的顶部对齐

(3)其底部与 下部滚动文本框 的底部边缘对齐;并且,

(4)其右边缘与 屏幕 的右边缘对齐。

import tkinter
from tkinter import Button
from tkinter import scrolledtext as tkst
import numpy as np
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure


matplotlib.use('TkAgg')

def dummy():
    return

ww = tkinter.Tk()
ww.geometry('700x400')
ww.state('zoomed')

datalst = [31, 41, 59, 26, 53, 58, 97, 96, 36]
ff = Figure(figsize=(6,6), dpi=100)
xx = ff.add_subplot(111)
ind = np.arange(len(datalst))
rects1 = xx.bar(ind, datalst, 0.8)
canvas = FigureCanvasTkAgg(ff, master=ww)
canvas.draw()
canvas.get_tk_widget().pack()

butt1 = Button(ww,text='Button 1',command=dummy, height=1,width=20,state='normal')
butt1.place(x=12, y=2)
butt2 = Button(ww,text='Button 2',command=dummy, height=1,width=20,state='disabled')
butt2.place(x=162, y=2)
butt3 = Button(ww,text='Button 3',command=dummy, height=1,width=10,state='disabled')
butt3.place(x=320, y=100)
butt4 = Button(ww,text='Button 4',command=dummy, height=1,width=10,state='disabled')
butt4.place(x=320, y=128)
butt5 = Button(ww,text='Button 5',command=dummy, height=1,width=10,state='disabled')
butt5.place(x=320, y=156)
butt6 = Button(ww,text='Button 6',command=dummy, height=1,width=10,state='disabled')
butt6.place(x=320, y=184)
butt7 = Button(ww,text='Button 7',command=dummy, height=1,width=12,state='disabled')
butt7.place(x=420, y=394)
butt8 = Button(ww,text='Button 8',command=dummy, height=1,width=12,state='disabled')
butt8.place(x=600, y=394)

disp_txt1 = tkst.ScrolledText(ww, width=36, height=12, wrap=tkinter.WORD, state='disabled')
disp_txt1.pack(fill=tkinter.BOTH)
disp_txt1.place(x=410, y=100)
disp_txt2 = tkst.ScrolledText(ww, width=36, height=5, wrap=tkinter.WORD, state='disabled')
disp_txt2.pack(fill=tkinter.BOTH)
disp_txt2.place(x=410, y=300)

ww.mainloop()

下面的橙色矩形显示了我希望图形位于的位置:

enter image description here

1 个答案:

答案 0 :(得分:1)

要执行此操作,请导入tkinter as tk。请注意,以这种方式导入tkinter总是很好,因为它不会导入namespaces importing tkinter as tk

这不是错误,您需要指定widget放置在pack的位置,将其放置在窗口中可用空间的中间。 为此,请使用canvas.get_tk_widget().pack(side=tk.RIGHT),它表示屏幕的右侧。您可以查看文档中的属性。

完整代码

from tkinter import scrolledtext as tkst
import numpy as np
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import tkinter as tk


matplotlib.use('TkAgg')

def dummy():
    return

ww = tk.Tk()
ww.geometry('700x400')
ww.state('zoomed')

datalst = [31, 41, 59, 26, 53, 58, 97, 96, 36]
ff = Figure(figsize=(6,6), dpi=100)
xx = ff.add_subplot(111)
ind = np.arange(len(datalst))
rects1 = xx.bar(ind, datalst, 0.8)
canvas = FigureCanvasTkAgg(ff, master=ww)
canvas.draw()
canvas.get_tk_widget().pack(side=tk.RIGHT)

butt1 = tk.Button(ww,text='Button 1',command=dummy, height=1,width=20,state='normal')
butt1.place(x=12, y=2)
butt2 = tk.Button(ww,text='Button 2',command=dummy, height=1,width=20,state='disabled')
butt2.place(x=162, y=2)
butt3 = tk.Button(ww,text='Button 3',command=dummy, height=1,width=10,state='disabled')
butt3.place(x=320, y=100)
butt4 = tk.Button(ww,text='Button 4',command=dummy, height=1,width=10,state='disabled')
butt4.place(x=320, y=128)
butt5 = tk.Button(ww,text='Button 5',command=dummy, height=1,width=10,state='disabled')
butt5.place(x=320, y=156)
butt6 = tk.Button(ww,text='Button 6',command=dummy, height=1,width=10,state='disabled')
butt6.place(x=320, y=184)
butt7 = tk.Button(ww,text='Button 7',command=dummy, height=1,width=12,state='disabled')
butt7.place(x=420, y=394)
butt8 = tk.Button(ww,text='Button 8',command=dummy, height=1,width=12,state='disabled')
butt8.place(x=600, y=394)

disp_txt1 = tkst.ScrolledText(ww, width=36, height=12, wrap=tk.WORD, state='disabled')
disp_txt1.pack(fill=tk.BOTH)
disp_txt1.place(x=410, y=100)
disp_txt2 = tkst.ScrolledText(ww, width=36, height=5, wrap=tk.WORD, state='disabled')
disp_txt2.pack(fill=tk.BOTH)
disp_txt2.place(x=410, y=300)

ww.mainloop()

enter image description here